-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq4p4.java
More file actions
70 lines (66 loc) · 2.08 KB
/
Copy pathq4p4.java
File metadata and controls
70 lines (66 loc) · 2.08 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.lang.*;
import java.util.*;
public class q4p4{
public static void main(String[] args){
List<LinkedList> list = new ArrayList<>();
LinkedList<Node> q = new LinkedList<>();
int[] input = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
Node root = buildTree(input, 0, input.length-1);
preOrder(root);
System.out.println("H >> " + checkHeight(root));
int res = checkBalance(root);
if(Integer.MIN_VALUE == res)
System.out.println("No Banlance!!");
else{
System.out.println("Balance");
System.out.println(res);
}
}
public static Node buildTree(int[] arr, int start, int end){
if(start > end) return null;
int mid = (start + end)/2;
Node root = new Node(arr[mid]);
root.left = buildTree(arr, start, mid-1);
root.right = buildTree(arr, mid+1, end);
return root;
}
public static void preOrder(Node root){
if(root == null){
System.out.println("x");
return;
}
System.out.println(root.value);
preOrder(root.left);
preOrder(root.right);
}
public static int checkHeight(Node root){
if(root == null) return -1; // why -1, leaf has no child.
return Math.max(checkHeight(root.left), checkHeight(root.right))+1;
}
//T: O(n)
//S: O(h) h is the tree height
public static int checkBalance(Node root){
int error = Integer.MIN_VALUE;
if(root == null) return -1;
int leftHeight = 0;
int rightHeight = 0;
leftHeight = checkBalance(root.left);
if(leftHeight == error)
return error;
rightHeight = checkBalance(root.right);
if(rightHeight == error)
return error;
if(Math.abs(leftHeight-rightHeight) > 1)
return error;
else
return Math.max(leftHeight,rightHeight)+1;
}
}
class Node{
public int value;
public Node left = null;
public Node right = null;
public Node(int value){
this.value = value;
}
}