平衡二叉树的判断

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

平衡二叉树左右子树深度只差不超过1.

public class Solution {
    
    private boolean isAVL = true;
    
    public boolean IsBalanced_Solution(TreeNode root) {
        
        if (root == null)
            return true;
        
        depth(root);
        return isAVL;
    }
    
    public int depth(TreeNode root) {
        if (root == null)
            return 0;
        int leftDepth = depth(root.left);
        int rightDepth = depth(root.right);
        if (leftDepth - rightDepth > 1 || leftDepth - rightDepth < -1) {
            isAVL = false;
        }
        return leftDepth>rightDepth ? leftDepth+1:rightDepth+1;
    }
}

 

posted @ 2016-09-01 20:04  Pickle  阅读(301)  评论(0编辑  收藏  举报