Construct a Full Binary Tree from Preorder and Mirror Preorder Traversal (Java)
Problem Statement
You are given two arrays:
pre[]→ Preorder traversal of the original full binary tree.preMirror[]→ Preorder traversal of the mirror of the same tree.
Your task is to construct the original full binary tree.
Note: The tree is guaranteed to be a Full Binary Tree, which means every node has either:
0 children (leaf node), or
2 children (internal node).
This property makes the reconstruction unique.
Understanding the Traversals
Original Tree Preorder
Preorder traversal follows:
Root → Left → Right
Example
1
/ \
2 3
/ \ / \
4 5 6 7
Preorder
1 2 4 5 3 6 7
Mirror Tree
A mirror tree swaps every left child with its right child.
1
/ \
3 2
/ \ / \
7 6 5 4
Mirror preorder also follows:
Root → Left → Right
So the mirror preorder becomes:
1 3 7 6 2 5 4
Key Observation
The first element of both arrays is always the root.
pre
1 2 4 5 3 6 7
^
preMirror
1 3 7 6 2 5 4
^
So,
Root = 1
What Comes Next?
The second element in preorder is always the left child.
pre
1 2 4 5 3 6 7
^
So,
Left Child = 2
Now search this node in the mirror preorder.
preMirror
1 3 7 6 2 5 4
^
Node 2 is found at index 4.
This index helps determine where the left subtree begins and ends.
Why Does This Work?
Mirror preorder visits nodes in the following order:
Root
Right Subtree
Left Subtree
When we find node 2, everything before it belongs to the right subtree.
Everything from index 4 onward belongs to the left subtree.
This allows us to recursively construct the tree.
Step-by-Step Algorithm
Step 1
Store every element of preMirror inside a HashMap.
Value → Index
1 → 0
3 → 1
7 → 2
6 → 3
2 → 4
5 → 5
4 → 6
This lets us find any node in O(1) time.
Step 2
Maintain one global variable:
preIndex
Initially,
preIndex = 0
It always points to the next node to create.
Step 3
Create the root.
Node root = new Node(pre[preIndex]);
Increment:
preIndex++;
Step 4
If only one node exists:
l == r
Return the node.
It is a leaf node.
Step 5
Otherwise, find the next preorder element inside the mirror preorder.
idx = map.get(pre[preIndex]);
This tells us where the left subtree starts.
Step 6
Recursively construct:
root.leftroot.right
Complete Code
class Solution {
HashMap<Integer, Integer> map;
int preIndex;
public Node constructBinaryTree(int[] pre, int[] preMirror) {
map = new HashMap<>();
for (int i = 0; i < preMirror.length; i++) {
map.put(preMirror[i], i);
}
preIndex = 0;
return build(pre, 0, preMirror.length - 1);
}
private Node build(int[] pre, int l, int r) {
if (preIndex >= pre.length || l > r)
return null;
Node root = new Node(pre[preIndex++]);
if (l == r || preIndex >= pre.length)
return root;
int idx = map.get(pre[preIndex]);
root.left = build(pre, idx, r);
root.right = build(pre, l + 1, idx - 1);
return root;
}
}
Line-by-Line Explanation
Variables
HashMap<Integer, Integer> map;

Join the conversation! Your thoughts help the community grow.