剑指 Offer-55-I/55-II-二叉树的深度/平衡二叉树C++

-------------------------------------------------二刷2021/2/4------------------------------------------
这题二刷的时候有错误的想法

/**
 * 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) {
        return getDepth(root) != -1;
    }
    int getDepth(TreeNode* root) {
        if(root == NULL) return 0;
        int left = getDepth(root -> left);
       // if(left == -1) return -1;
        int right = getDepth(root -> right);
       // if(right == -1) return -1;
        if(left - right > 1 || right - left > 1) return -1;
        return max(left, right) + 1;
    }
};

没有保存 -1返回给isBalanced函数而是作为一个递归中间值继续递归。
应该补上被注释的语句。才能将子节点 -1 信息一层一层传达给根结点


55-I

题目描述

在这里插入图片描述

思路 前序遍历

/**
 * 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 depth = 1;
    int max = 0;
    int maxDepth(TreeNode* root) {
        dfs(root, 1);
        return max;
    }
    void dfs(TreeNode* root, int d) {
        if(root == NULL) return;
        max = d > max ? d : max;
        dfs(root -> left, d + 1);
        dfs(root -> right, d + 1);
    }
};

也可以后序遍历

    if(root == null) return 0;
    return max(maxDepth(root _> left), maxDepth(root -> right)) + 1;

时间复杂度O(N)
空间复杂度O(N)

55-II

题目描述

在这里插入图片描述

思路 前序遍历+判断深度+剪枝

/**
 * 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 == NULL) return true;
        if(compare(root) == -1) return false;
        return  isBalanced(root -> left) && isBalanced(root -> right);
    }
    int compare(TreeNode* root) {
        if(root == NULL) return 0;
        int left = compare(root -> left);
        int right = compare(root -> right);
        if(abs(left - right) > 1) return -1;
        return max(left, right) + 1;
    }
};

时间复杂度O(NlogN)
空间复杂度O(N) :退化为链表
上述代码其实很差
首先剪枝还不够好
当右子树发现为-1时就不需要判断左子树了.
其次其实我们在遍历计算左右子树高度的时候,在回溯过程中就可以顺便比较左右子树的高度差,
所以return isBalanced(root -> left) && isBalanced(root -> right);是没有意义的

/**
 * 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) {
        return compare(root) != -1;
    }
    int compare(TreeNode* root) {
        if(root == NULL) return 0;
        int left = compare(root -> left);
        if(left == -1) return -1;
        int right = compare(root -> right);
        if(right == -1) return -1;
        if(abs(left - right) > 1) return -1;
        return max(left, right) + 1;
    }
};

时间复杂度O(N)
空间复杂度O(N) :退化为链表

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值