Given a binary tree, determine if it is height-balanced.
Height-balanced binary tree : is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
Return 0 / 1 ( 0 for false, 1 for true ) for this problem
Example :
Input :
1
/ \
2 3
Return : True or 1
Input 2 :
3
/
2
/
1
Return : False or 0
Because for the root node, left subtree has depth 2 and right subtree has depth 0.
Difference = 2 > 1.
Method:
DFS
Solution:
Time: O(n) Space: O(n)
/** * Definition for binary tree * class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { * val = x; * left=null; * right=null; * } * } */ public class Solution { public int isBalanced(TreeNode A) { if (A == null) return 1; int left = depth(A.left); int right = depth(A.right); if (left == -1 || right == -1) { return 0; } return Math.abs(left - right) <= 1 ? 1 : 0; }
private int depth(TreeNode A) { if (A == null) return 0; if (A.left == null && A.right == null) return 1; int left = depth(A.left); int right = depth(A.right); if (left == -1 || right == -1) { return -1; } return Math.abs(left - right) <= 1 ? Math.max(left, right) + 1 : -1; } }