Skip to content

Commit eb97573

Browse files
110. Balanced Binary Tree (java)
1 parent de52797 commit eb97573

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public boolean isBalanced(TreeNode root) {
3+
return depth(root) != -1;
4+
}
5+
private int depth(TreeNode root) {
6+
if (root == null) return 0;
7+
int left = depth(root.left);
8+
if (left == -1) return -1;
9+
int right = depth(root.right);
10+
if (right == -1 || Math.abs(left - right) > 1) return -1;
11+
return Math.max(left, right) + 1;
12+
}
13+
}

0 commit comments

Comments
 (0)