-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathBalancedTree.java
More file actions
43 lines (35 loc) · 786 Bytes
/
BalancedTree.java
File metadata and controls
43 lines (35 loc) · 786 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
38
39
40
41
42
43
package Trees;
/**
* Author - archit.s
* Date - 05/11/18
* Time - 11:19 AM
*/
public class BalancedTree {
class Height{
int d;
public Height(int h){
this.d = h;
}
}
public boolean isBalanced(TreeNode A, Height h){
if(A == null){
h.d = 0;
return true;
}
Height l = new Height(0);
Height r = new Height(0);
if(!isBalanced(A.left, l) || !isBalanced(A.right, r)){
return false;
}
h.d = Math.max(l.d,r.d) + 1;
if(Math.abs(l.d - r.d) <=1){
return true;
}
else{
return false;
}
}
public int isBalanced(TreeNode A) {
return isBalanced(A, new Height(0)) ? 1 : 0;
}
}