39.判断是不是平衡二叉树

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

public class Solution {
    private boolean flag = true;
    
    public boolean IsBalanced_Solution(TreeNode root) {
        getDepth(root);
        return flag;
    }
    
    public int getDepth(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int left = getDepth(root.left);
        int right = getDepth(root.right);
        
        if(Math.abs(left - right) > 1) {
            flag = false;
        }
        return right>left?right+1:left+1;
    }
}

 

posted on 2019-04-07 15:57  归臻  阅读(77)  评论(0)    收藏  举报

导航