-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
32 lines (27 loc) · 748 Bytes
/
Solution.java
File metadata and controls
32 lines (27 loc) · 748 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package leetCode_110;
/**
* @author dimdark
* @date 2017-09-04
* @time 8:20 AM
*/
public class Solution {
static class TreeNode {
int val;
TreeNode left, right;
TreeNode(int val) {
this.val = val;
}
}
private int balanceDepth(TreeNode root) {
if (root == null) return 0;
int leftDepth = balanceDepth(root.left);
if (leftDepth == -1) return -1;
int rightDepth = balanceDepth(root.right);
if (rightDepth == -1) return -1;
if (Math.abs(leftDepth - rightDepth) > 1) return -1;
else return Integer.max(leftDepth, rightDepth) + 1;
}
public boolean isBalanced(TreeNode root) {
return balanceDepth(root) != -1;
}
}