LeetCode 110: Balanced Binary Tree

/**
 * 110. Balanced Binary Tree
 * 1. Time:O(n)  Space:O(logn)
 */

// 1. Time:O(n)  Space:O(logn)
class Solution {
    public boolean isBalanced(TreeNode root) {
        return helper(root)>=0;
    }
    
    public int helper(TreeNode root){
        if(root==null) return 0;
        int left = helper(root.left);
        int right = helper(root.right);
        if(left<0 || right<0 || Math.abs(left-right)>1) return -1;
        return Math.max(left,right)+1;
    }
}
posted @ 2020-04-29 11:13  AAAmsl  阅读(72)  评论(0编辑  收藏  举报