其他分享
首页 > 其他分享> > 剑指offer-判断是否是平衡二叉树

剑指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);
    }

 

标签:right,offer,int,是否是,height,isBalanced,二叉树,root,left
来源: https://www.cnblogs.com/Roni-i/p/10346633.html