【剑指Offer】55.2 平衡二叉树

NowCoder

题目描述

平衡二叉树左右子树高度差不超过 1。
在这里插入图片描述

public class Solution {
    // 求深度
    public int TreeDepth(TreeNode root) {
        return root == null ? 0 : Math.max(TreeDepth(root.left)+1, TreeDepth(root.right)+1);
    }
    public boolean IsBalanced_Solution(TreeNode root) {
        if(root == null)
            return true;
        
        int left = TreeDepth(root.left);
        int right = TreeDepth(root.right);
        return Math.abs(left - right) > 1 ? false : true && IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right);
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43469680/article/details/108301281