LeetCode——树——递归
1、树的高度
T.104 Maximum Depth of Binary Tree (Easy)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
if (!root)
return 0;
return max(maxDepth(root->left), maxDepth(root->right)) + 1;
}
};
2、平衡树
T110 Balanced Binary Tree (Easy)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isBalanced(TreeNode* root) {
if (!root)
return true;
if (abs(getDepth(root->left) - getDepth(root->right)) > 1)
return false;
return isBalanced(root->left) && isBalanced(root->right);
}
int getDepth(TreeNode* root){
if (!root)
return 0;
return(max(getDepth(root->left), getDepth(root->right)) + 1);
}
};
3、两节点的最长路径
T. 543 Diameter of Binary Tree (Easy)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int diameterOfBinaryTree(TreeNode* root) {
int res = 0;
depth(root, res);
return res;
}
int depth(TreeNode* root, int& res){
if(!root)
return 0;
int L = depth(root->left, res);
int R = depth(root->right, res);
res = max(res, L+R);
return max(L, R)+1;
}
};
4、翻转树
T.226 Invert Binary Tree (Easy)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(!root)
return NULL;
TreeNode* tmp = root->left;
root->left = invertTree(root->right);
root->right = invertTree(tmp);
return root;
}
};
5、归并两棵树
T.617 Merge Two Binary Trees (Easy)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
if(!t1)
return t2;
if(!t2)
return t1;
TreeNode* t = new TreeNode(t1->val + t2->val);
t->left = mergeTrees(t1->left, t2->left);
t->right = mergeTrees(t1->right, t2->right);
return t;
}
};
6、判断路径和是否等于一个数
T.112 Path Sum (Easy)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool hasPathSum(TreeNode* root, int sum) {
if(!root)
return false;
if(!root->left && !root->right && root->val == sum)
return true;
return hasPathSum(root->left, sum-root->val) || hasPathSum(root->right, sum-root->val);
}
};
7、统计路径和等于一个数的路径数量
T.437 Path Sum III (Easy)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int pathSum(TreeNode* root, int sum) {
if(!root)
return 0;
return sumUp(root, 0, sum) + pathSum(root->left, sum) + pathSum(root->right, sum);
}
int sumUp(TreeNode* root, int pre, int& sum){
if(!root)
return 0;
int cur = pre + root->val;
return (cur == sum) + sumUp(root->left, cur, sum) + sumUp(root->right, cur, sum);
}
};
8、子树
T.572 Subtree of Another Tree (Easy)
/**
* 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 isSubtree(TreeNode* s, TreeNode* t) {
if(!s)
return false;
if(isSame(s, t))
return true;
return isSubtree(s->left, t) || isSubtree(s->right, t);
}
bool isSame(TreeNode* s, TreeNode* t){
if(!s && !t)
return true;
if(!s || !t)
return false;
if(s->val != t->val)
return false;
return isSame(s->left, t->left) && isSame(s->right, t->right);
}
};
9、树的对称
T.101 Symmetric Tree (Easy)
法一:递归
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(!root)
return true;
return isSymmetric(root->left, root->right);
}
bool isSymmetric(TreeNode* left, TreeNode* right){
if (!left && !right)
return true;
if (left && !right || !left && right || left->val != right->val)
return false;
return isSymmetric(left->left, right->right) && isSymmetric(left->right, right->left);
}
};
法二:迭代
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(!root)
return true;
queue<TreeNode*> q1,q2;
q1.push(root->left);
q2.push(root->right);
while(!q1.empty() && !q2.empty()){
TreeNode* node1 = q1.front();
q1.pop();
TreeNode* node2 = q2.front();
q2.pop();
if(!node1 && !node2)
continue;
if(!node1 && node2)
return false;
if(node1 && !node2)
return false;
if(node1->val != node2->val)
return false;
q1.push(node1->left);
q1.push(node1->right);
q2.push(node2->right);
q2.push(node2->left);
}
return true;
}
};
10、最小路径
T.111 Minimum Depth of Binary Tree (Easy)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode* root) {
if(!root)
return 0;
if(!root->left)
return 1 + minDepth(root->right);
if(!root->right)
return 1 + minDepth(root->left);
return 1 + min(minDepth(root->left), minDepth(root->right));
}
};
11、统计左叶子节点的和
T.404 Sum of Left Leaves (Easy)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
if(!root)
return 0;
if(root->left && !root->left->left && !root->left->right)
return root->left->val + sumOfLeftLeaves(root->right);
return sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right);
}
};
12、相同节点值的最大路径长度
T.687 Longest Univalue Path (Easy)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int longestUnivaluePath(TreeNode* root) {
int res = 0;
UnivaluePath(root, res);
return res;
}
int UnivaluePath(TreeNode* node, int& res) {
if (!node) return 0;
int l = UnivaluePath(node->left, res);
int r = UnivaluePath(node->right, res);
int pl = 0;
int pr = 0;
if(node->left && node->val == node->left->val)
pl = l + 1;
if(node->right && node->val == node->right->val)
pr = r + 1;
res = max(res, pl + pr);
return max(pl, pr);
}
};
13、间隔遍历
T.337 House Robber III (Medium)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int rob(TreeNode* root) {
vector<int> res = dfs(root);
return max(res[0], res[1]);
}
vector<int> dfs(TreeNode* root){
if(!root)
return vector<int> (2,0);
vector<int> left = dfs(root->left);
vector<int> right = dfs(root->right);
vector<int> res(2,0);
res[0] = max(left[0], left[1]) + max(right[0], right[1]);
res[1] = left[0] + right[0] + root->val;
return res;
}
};
14、找出二叉树中第二小的节点
T.671 Second Minimum Node In a Binary Tree (Easy)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int findSecondMinimumValue(TreeNode* root) {
return helper(root, root->val);
}
int helper(TreeNode* node, int first) {
if (!node)
return -1;
if (node->val != first)
return node->val;
int left = helper(node->left, first), right = helper(node->right, first);
return (left == -1 || right == -1) ? max(left, right) : min(left, right);
}
};