-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstructTreeIP.java
More file actions
48 lines (46 loc) · 1.29 KB
/
ConstructTreeIP.java
File metadata and controls
48 lines (46 loc) · 1.29 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
public class ConstructTreeIP {
static class Node {
int data;
Node left, right;
Node(int data) {
this.data = data;
this.left = null;
this.right = null;
}
}
static int in[], pr[];
static Node rt = null;
public static Node constTree(int lI, int hI, int lP, int hP) {
if(lI > hI || lP > hP) return null;
Node root = new Node(pr[lP]);
if(rt == null) rt = root;
int p = 0;
for(int i =lI;i<=hI;i++) {
if(in[i] == pr[lP]) {
p = i;
break;
}
}
int leftLen = 0;
if((p-lI) > 0) leftLen = p-lI;
root.left = constTree(lI, lI+leftLen-1, lP+1, lP+leftLen);
root.right = constTree( p+1, hI, lP+leftLen+1,hP);
return root;
}
static void postOrder(Node root) {
if(root == null) return;
postOrder(root.left);
postOrder(root.right);
System.out.print(root.data + " ");
}
public static void main(String args[]) {
int io[] = {3, 1, 4, 0, 5, 2};
in = io;
int po[] = {0, 1, 3, 4, 2, 5};
pr = po;
constTree( 0, 5, 0, 5);
postOrder(rt);
//System.out.print(rt.left.data + " ");
System.out.println();
}
}