[LeetCode]Balanced Binary Tree

public class Solution {
    boolean result = true;
    public boolean isBalanced(TreeNode root) {
        helper(root);
        return result;
    }
    public int helper(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = helper(root.left);
        int right = helper(root.right);
        if (Math.abs(left - right) > 1) {
            result = false;
            return -1;
        } else {
            return Math.max(left, right) + 1;
        }
    }
}

 

posted @ 2015-12-01 15:28  Weizheng_Love_Coding  阅读(104)  评论(0编辑  收藏  举报