-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq4p2.java
More file actions
35 lines (32 loc) · 907 Bytes
/
Copy pathq4p2.java
File metadata and controls
35 lines (32 loc) · 907 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
public class q4p2{
public static void main(String[] args){
int[] input = {1,2,3,4,5,6,7,8,9,10,11};
Node root = buildTree(input, 0, input.length-1);
preOrder(root);
}
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);
}
}
class Node{
public int value;
public Node left = null;
public Node right = null;
public Node(int value){
this.value = value;
}
}