110.平衡二叉树
思路:后序遍历计算高度
代码:
/**
* 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 getHeight(TreeNode* node){
if(node==NULL) return 0;
int leftHeight=getHeight(node->left);
if(leftHeight==-1) return -1;
int rightHeight=getHeight(node->right);
if(rightHeight==-1) return -1;
if(abs(leftHeight-rightHeight)>1)
return -1;
return 1+max(leftHeight,rightHeight);
}
bool isBalanced(TreeNode* root) {
if(getHeight(root)==-1)
return false;
else
return true;
}
};
257. 二叉树的所有路径
思路:保存路径的字符串参数使用复制构造起到回溯的效果
代码:
/**
* 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:
void traversal(TreeNode* node,string path,vector<string>& res){
path+=(to_string(node->val));
if(node->left==NULL&&node->right==NULL){
res.push_back(path);
return;
}
if(node->left){
traversal(node->left,path+"->",res);
}
if(node->right){
traversal(node->right,path+"->",res);
}
return;
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
if(root==NULL)
return res;
string path;
traversal(root,path,res);
return res;
}
};
404.左叶子之和
思路:后序遍历,通过父亲结点来判断字结点是否为左叶子
代码:
/**
* 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 == 0)
return 0;
int leftsum = sumOfLeftLeaves(root->left);
int rightsum = sumOfLeftLeaves(root->right);
if(root->left&&!root->left->left&&!root->left->right)
leftsum=root->left->val;
return leftsum + rightsum;
}
};
222.完全二叉树的节点个数
思路:完全二叉树的性质,满二叉树的结点个数为2的n次方-1
代码:
/**
* 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 == nullptr) return 0;
TreeNode* left = root->left;
TreeNode* right = root->right;
int leftDepth = 0, rightDepth = 0; // 这里初始为0是有目的的,为了下面求指数方便
while (left) { // 求左子树深度
left = left->left;
leftDepth++;
}
while (right) { // 求右子树深度
right = right->right;
rightDepth++;
}
if (leftDepth == rightDepth) {
return (2 << leftDepth) - 1; // 注意(2<<1) 相当于2^2,所以leftDepth初始为0
}
return countNodes(root->left) + countNodes(root->right) + 1;
}
};
标签:right,TreeNode,val,int,root,随想录,二叉树,第十五天,left
From: https://blog.csdn.net/dtgfhfd/article/details/140502293