剑指offer 二叉平衡树

平衡二叉树

http://www.nowcoder.com/questionTerminal/8b3b95850edb4115918ecebdf1b4d222

双重递归(利用上一题的结果)

public class Solution {
    public int TreeDepth(TreeNode root) {
        if (root == null) return 0;
        return 1 + Math.max(TreeDepth(root.left), TreeDepth(root.right));
    }

    public boolean IsBalanced_Solution(TreeNode root) {
        if (root == null) return true;
        return Math.abs(TreeDepth(root.left) - TreeDepth(root.right)) <= 1
            && IsBalanced_Solution(root.left)
            && IsBalanced_Solution(root.right);
    }
}
全部评论
您好,请问为什么不用判断左右两端大小,而只判断高度,平衡二叉树本身不也应该是二叉搜索树吗
点赞 回复 分享
发布于 2020-04-05 22:06

相关推荐

05-09 13:22
门头沟学院 Java
点赞 评论 收藏
分享
评论
3
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务