欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > 代码随想录day15

代码随想录day15

2025/4/24 8:31:16 来源:https://blog.csdn.net/m0_74045500/article/details/145312370  浏览:    关键词:代码随想录day15

110.

知道平衡二叉树的概念即可。

/** @lc app=leetcode.cn id=110 lang=cpp** [110] 平衡二叉树*/// @lc code=start
/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
#include<iostream>
using namespace std;
class Solution {
public:int getHeight(TreeNode* node){if(node==NULL)return 0;int left=getHeight(node->left);if(left==-1)return -1;int right=getHeight(node->right);if(right==-1)return -1;return abs(right-left)>1?-1:1+max(right,left);}bool isBalanced(TreeNode* root) {return getHeight(root)==-1?false:true;}
};
// @lc code=end

257. 

/** @lc app=leetcode.cn id=257 lang=cpp** [257] 二叉树的所有路径*/// @lc code=start
/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:void travasal(TreeNode* cur,string path,vector<string>& result){path+=to_string(cur->val);if (cur->left == NULL && cur->right == NULL) {result.push_back(path);return;}if (cur->left) travasal(cur->left, path + "->", result); // 左if (cur->right) travasal(cur->right, path + "->", result); // 右}vector<string> binaryTreePaths(TreeNode* root) {vector<string> result;string path;if (root == NULL) return result;travasal(root, path, result);return result;}
};
// @lc code=end

404.

/** @lc app=leetcode.cn id=404 lang=cpp** [404] 左叶子之和*/// @lc code=start
/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:int sumOfLeftLeaves(TreeNode* root) {if (root == NULL) return 0;if (root->left == NULL && root->right== NULL) return 0;int leftValue = sumOfLeftLeaves(root->left);    if (root->left && !root->left->left && !root->left->right) { // 左子树就是一个左叶子的情况leftValue = root->left->val;}int rightValue = sumOfLeftLeaves(root->right);  int sum = leftValue + rightValue;              return sum;}
};
// @lc code=end

222.

/** @lc app=leetcode.cn id=222 lang=cpp** [222] 完全二叉树的节点个数*/// @lc code=start
/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:int countNodes(TreeNode* root) {if (root == NULL) return 0;return 1 + countNodes(root->left) + countNodes(root->right);}
};
// @lc code=end

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词