forked from JsonChao/Awesome-Algorithm-Study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution110.java
More file actions
37 lines (28 loc) · 697 Bytes
/
Copy pathSolution110.java
File metadata and controls
37 lines (28 loc) · 697 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
33
34
35
36
37
package binary_search_tree_problem;
public class Solution110 {
// Definition for a binary tree node.
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
private boolean result = true;
public boolean isBalanced(TreeNode root) {
maxDepth(root);
return result;
}
private int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
int l = maxDepth(root.left);
int r = maxDepth(root.right);
if (Math.abs(l - r) > 1) {
result = false;
}
return 1 + Math.max(l , r);
}
}