-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathBinaryTreeConstructor.java
72 lines (65 loc) · 2.05 KB
/
BinaryTreeConstructor.java
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
71
72
package com.pavan.Trees;
import java.util.Stack;
public class BinaryTreeConstructor {
public static class Node{
int data;
Node left;
Node right;
Node(int data , Node left, Node right) {
this.data = data;
this.left = left;
this.right = right;
}
}
public static class Pair{
Node node;
int state;
Pair(Node node, int state){
this.node = node;
this.state = state;
}
}
public static void display(Node node){
if(node == null) return;
String str = "";
str += node.left == null ? "." : node.left.data + "";
str += " <- " + node.data + " -> ";
str += node.right == null ? "." : node.right.data+"";
System.out.println(str);
display(node.left);
display(node.right);
}
public static void main(String[] args) {
Integer [] arr = {50,12,25,null,null,37,30,null,null,null,75,62,null,70,null,null,87,null,null};
Stack<Pair> st = new Stack<Pair>();
Node root = new Node(arr[0] ,null,null);
Pair rootPair = new Pair(root, 1 );
st.push(rootPair);
int idx = 1;
while(st.size() > 0){
Pair top = st.peek();
if(top.state == 1){
if(arr[idx] != null){
Node leftchild = new Node(arr[idx],null,null);
top.node.left = leftchild;
st.push(new Pair(leftchild,1));
}
top.state++;
idx++;
}
else if(top.state == 2){
if(arr[idx] != null){
Node rightchild = new Node(arr[idx],null,null);
top.node.right = rightchild;
st.push(new Pair(rightchild,1));
}
top.state++;
idx++;
}
else{
st.pop();
}
}
display(root);
}
}