平衡二叉树
递归法:
这题虽说是深度,但是还是要求左右子树的高度,因为如果使用前序遍历,在没有遍历到空节点的时候是不知道该节点的深度的,没有遍历到需要return返回的条件就进行判断的话,是无法知道左子树或右子树的深度的,所以需要用后序遍历来求左右子树的高度
/**
* 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* cur) {
if (cur == nullptr)
return 0;
int left = getHeight(cur->left);
int right = getHeight(cur->right);
if (left == -1 || right == -1)
return -1;
if (abs(left - right) > 1)
return -1;
else {
return 1 + max(left, right);
}
}
bool isBalanced(TreeNode* root) {
int res = getHeight(root);
return res != -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:
vector<string> res;
void getPath(TreeNode* cur, string s) {
if (cur->left == nullptr && cur->right == nullptr) {
res.push_back(s);
return;
}
if (cur->left)
getPath(cur->left, s + "->" + to_string(cur->left->val));
if (cur->right)
getPath(cur->right, s + "->" + to_string(cur->right->val));
}
vector<string> binaryTreePaths(TreeNode* root) {
getPath(root, to_string(root->val));
return res;
}
};
相同的树
同时遍历左树和右树即可
/**
* 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:
bool isSameTree(TreeNode* p, TreeNode* q) {
if (p == nullptr && q != nullptr)
return false;
if (p != nullptr && q == nullptr)
return false;
if (p == nullptr && q == nullptr)
return true;
if (p->val != q->val)
return false;
bool left = isSameTree(p->left, q->left);
bool right = isSameTree(p->right, q->right);
return left && right;
}
};
另一颗树的子树
在相同的树代码的基础上,使用前序遍历依次判断以某个节点为根节点的子树是否和题目所给子树相同即可
/**
* 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:
bool compare(TreeNode* left, TreeNode* right) {
if (left == NULL && right != NULL)
return false;
else if (left != NULL && right == NULL)
return false;
else if (left == NULL && right == NULL)
return true;
// 排除了空节点,再排除数值不相同的情况
else if (left->val != right->val)
return false;
bool outside = compare(
left->left,
right->left); // 左子树:左、 右子树:左
// (相对于求对称二叉树,只需改一下这里的顺序)
bool inside =
compare(left->right, right->right); // 左子树:右、 右子树:右
bool isSame = outside && inside; // 左子树:中、 右子树:中 (逻辑处理)
return isSame;
}
bool isSubtree(TreeNode* root, TreeNode* subRoot) {
if (root == nullptr && subRoot != nullptr)
return false;
if (root != nullptr && subRoot == nullptr)
return false;
if (root == nullptr && subRoot == nullptr)
return true;
if (root != nullptr && subRoot != nullptr) {
if (compare(root, subRoot))
return true;
}
return isSubtree(root->left, subRoot) ||
isSubtree(root->right, subRoot);
}
};
左叶子之和
给朝左遍历的节点定上一个标记,如果递归到了叶子节点且标记为true,则将其值加入到sum里
/**
* 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 getSum(TreeNode* cur, int& sum, bool isLeft) {
if (cur->left == nullptr && cur->right == nullptr) {
if (isLeft)
sum += cur->val;
return;
}
if (cur->left)
getSum(cur->left, sum, true);
if (cur->right)
getSum(cur->right, sum, false);
}
int sumOfLeftLeaves(TreeNode* root) {
if (root == nullptr)
return 0;
int sum = 0;
getSum(root, sum, false);
return sum;
}
};
不打标记的情况下,只需要从左叶子的父节点开始判断即可
/**
* 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 == nullptr)
return 0;
TreeNode* leftnode = root->left;
int leftvalue = sumOfLeftLeaves(leftnode);
if (leftnode != nullptr && leftnode->left == nullptr &&
leftnode->right == nullptr)
leftvalue = leftnode->val;
int rightvalue = sumOfLeftLeaves(root->right);
return leftvalue + rightvalue;
}
};