LeetCode刷题——树——递归

本文详细介绍了LeetCode上关于树的递归操作的多个经典题目,包括树的高度、平衡树、两节点的最长路径、翻转树、归并两棵树等。通过这些题目,读者可以深入理解树的递归算法,提高解决树相关问题的能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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()