剑指Offer-二叉树-(9)

知识点/数据结构:二叉树

题目描述
输入一棵二叉树,判断该二叉树是否是平衡二叉树。

在这里插入图片描述

//一次编译通过!!!!!!!加油这是个信号。头条,等我!!!!!1
public class Solution {
    public boolean IsBalanced_Solution(TreeNode root) {
        if (root==null) return true;
        if(Math.abs(TreeDepth(root.left)-TreeDepth(root.right))>1)
        {
            return false;
        }
        return IsBalanced_Solution(root.left)&&IsBalanced_Solution(root.right);
    }
    public int TreeDepth(TreeNode root) {
        //2018.10.07找了大王八取经,梦想还是要有的。
        //2018.11.19最后一次的机会
            if(root==null){
            return 0;
        }
                                                             
        int nLelt=TreeDepth(root.left);
        int nRight=TreeDepth(root.right);
         
        return nLelt>nRight?(nLelt+1):(nRight+1);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_35649064/article/details/84874178
今日推荐