-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday5.java
More file actions
46 lines (39 loc) · 922 Bytes
/
Copy pathday5.java
File metadata and controls
46 lines (39 loc) · 922 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
44
45
46
public class Node {
Node left;
int val;
Node right;
public int min(Node root){
Node curr = root;
while(curr.left != null){
curr = curr.left;
}
return curr.val;
}
public int max(Node root){
Node curr = root;
while(curr.right != null){
curr = curr.right;
}
return curr.val;
}
public boolean isBalanced(Node root){
int lh;
int rh;
if (node == null) {
return true;
}
lh = height(node.left);
rh = height(node.right);
if (Math.Abs(lh - rh) <= 1 && isBalanced(node.left)
&& isBalanced(node.right)) {
return true;
}
return false;
}
public int height(Node nd){
if(nd == null){
return 0;
}
return 1 + Math.Max(height(nd.left),height(nd.right));
}
}