-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstructBinaryTreeFromInorderAndPreorder.java
More file actions
55 lines (51 loc) · 1.71 KB
/
ConstructBinaryTreeFromInorderAndPreorder.java
File metadata and controls
55 lines (51 loc) · 1.71 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
// https://www.interviewbit.com/problems/construct-binary-tree-from-inorder-and-preorder/
// #tree #binary-search-tree #binary-tree
/**
* Definition for binary tree class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int
* x) { val = x; left=null; right=null; } }
*/
public class Solution {
public TreeNode buildTree(int[] A, int[] B) {
// store order of each node
HashMap<Integer, Integer> order = new HashMap<>();
for (int i = 0; i < B.length; i++) {
order.put(B[i], i);
}
// build Binary Tree from Pre-order list with order of nodes
TreeNode root = new TreeNode(A[0]);
Deque<TreeNode> stack = new LinkedList<>();
stack.offerLast(root);
/*
for (int i = 1; i < A.length; i++) {
if (order.get(A[i]) < order.get(stack.peekLast().val)) {
TreeNode parent = stack.peekLast();
parent.left = new TreeNode(A[i]);
stack.offerLast(parent.left);
}
else {
TreeNode parent = stack.peekLast();
while(!stack.isEmpty() &&
(order.get(stack.peekLast().val) < order.get(A[i]))) {
parent = stack.pollLast();
}
parent.right = new TreeNode(A[i]);
stack.offerLast(parent.right);
}
}
*/
for (int i = 1; i < A.length; i++) {
TreeNode parent = stack.peekLast();
TreeNode node = new TreeNode(A[i]);
if (order.get(A[i]) < order.get(parent.val)) {
parent.left = node;
} else {
while (!stack.isEmpty() && (order.get(stack.peekLast().val) < order.get(A[i]))) {
parent = stack.pollLast();
}
parent.right = node;
}
stack.offerLast(node);
}
return root;
}
}