输入一棵二叉树判断二叉树是不是平衡二叉树

public boolean IsBalanced_Solution(TreeNode root) {
        if(root == null) return true;
        int left = getDepth(root.left);
        int right = getDepth(root.right);
        if(Math.abs(left-right)>1) return false;
        return IsBalanced_Solution(root.left)&&IsBalanced_Solution(root.right);
    }
    public int getDepth(TreeNode root){
        if(root == null) return 0;
        return Math.max(getDepth(root.left),getDepth(root.right))+1;
    }

 

posted @ 2016-09-01 10:32  樱圃  阅读(145)  评论(0编辑  收藏  举报