第3周 数据结构-树

本文深入探讨了二叉树的多种核心算法,包括计算树的高度、平衡性判断、求两节点间的最长路径等,并提供了详细的实现代码。通过递归方式解决这些问题,不仅介绍了基本原理,还分享了优化技巧。

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

递归

树的高度
class Solution {
public:
    int maxDepth(TreeNode* root) {
        /*
        思路:递归求子节点的最大深度,父节点的最大深度等于子节点最大深度+1
        时间复杂度:O(n)
        空间复杂度:O(n)
        */
        if(root == NULL)
            return 0;
        else
            return max(maxDepth(root->left), maxDepth(root->right)) + 1;
};
平衡树
class Solution {
public:
    int getMaxDepth(TreeNode* root){
        if(root == NULL)
            return 0;
        return max(getMaxDepth(root->left), getMaxDepth(root->right)) + 1;
    }
    bool isBalanced_multipass(TreeNode* root) {
        /*
        思路:计算节点的最大深度,先判断root节点是否平衡,若平衡再判断左右子节点是否平衡
        时间复杂度:O(n^2)
        空间复杂度:O(n^2)
        */
        if(root == NULL)
            return true;

        int leftDepth = getMaxDepth(root->left);
        int rightDepth = getMaxDepth(root->right);

        return abs(leftDepth - rightDepth) <= 1 && isBalanced(root->left) && isBalanced(root->right);
    }

    int maxdepth(TreeNode* root){
        if(root == NULL)
            return 0;
        int Lmax = maxdepth(root->left);
        if(Lmax == -1)
            return -1;
        int Rmax = maxdepth(root->right);
        if(Rmax == -1)
            return -1;
        if(abs(Lmax - Rmax) > 1)
            return -1;
        else
            return max(Lmax, Rmax) + 1;
    }
    bool isBalanced(TreeNode* root) {
        /*
        思路:在计算max depth的过程中就判断是否平衡,平衡则返回最大深度,否则返回-1
        时间复杂度:O(n)
        空间复杂度:O(n)
        */
        if(root == NULL)
            return true;
        return maxdepth(root) != -1;
    }
};
两节点的最长路径
class Solution {
public:
    int result;
    int maxdepth(TreeNode* root){
        if(root == NULL)
            return 0;
        int L = maxdepth(root->left);
        int R = maxdepth(root->right);
        if(L + R > result)
            result = L + R;
        return max(L, R) + 1;
    }
    int diameterOfBinaryTree(TreeNode* root) {
        /*
        思路:二叉树的直径等于左右子树的最大深度,所以在求最大深度的同时计算出节点的左右子树最大深度的和的最大值
        时间复杂度:O(n)
        空间复杂度:O(n)
        */
        result = 0;
        maxdepth(root);
        return result;
    }
};
翻转树
class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        /*
        思路:先翻转左右子树,然后在交换root的左右子树
        时间复杂度:O(n)
        空间复杂度:O(n)
        */
        if(root == NULL)
            return NULL;
        TreeNode* invertedLeft = invertTree(root->left);
        TreeNode* invertedRight = invertTree(root->right);
        root->left = invertedRight;
        root->right = invertedLeft;
        return root;
    }
};
归并两棵树
class Solution {
public:
    TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
        /*
        思路:先合并左右子树,然后将合并根节点
        时间复杂度:O(n)
        空间复杂度:O(n)
        */
        if(root1 == root2 && root1 == NULL)
            return NULL;
        if(root1 == NULL || root2 == NULL)
            return root1 == NULL ? root2 : root1;
        TreeNode* left = mergeTrees(root1->left, root2->left);
        TreeNode* right = mergeTrees(root1->right, root2->right);
        root1->val += root2->val;
        root1->left = left;
        root1->right = right;
        return root1;
    }
};
判断路径和是否等于一个数
class Solution {
public:
    bool hasPathSum(TreeNode* root, int targetSum) {
        /*
        思路:递归判断
        时间复杂度:O(n)
        空间复杂度:O(n)
        */
        if(root == NULL)
            return false;
        if(root->left == NULL && root->right == NULL)
            return targetSum == root->val;
        return hasPathSum(root->left, targetSum-root->val) || hasPathSum(root->right, targetSum-root->val);
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值