剑指offer-判断是否是平衡二叉树

 

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

 

posted @ 2019-02-01 18:21  Roni_i  阅读(99)  评论(0编辑  收藏  举报