VOOZH about

URL: https://www.geeksforgeeks.org/dsa/construct-full-binary-tree-using-preorder-traversal-preorder-traversal-mirror-tree/

⇱ Full Binary Tree using its Preorder and Mirror's Preorder - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Full Binary Tree using its Preorder and Mirror's Preorder

Last Updated : 27 May, 2026

Given two arrays pre[] and preMirror[] of size n containing unique elements, where pre[] represents the preorder traversal of a full binary tree and preMirror[] represents the preorder traversal of its mirror tree, construct the original full binary tree using these traversals.

Note: A general binary tree cannot be uniquely constructed using these two traversals. However, a full binary tree can be constructed uniquely from the given traversals without any ambiguity.

Examples:

Input: pre[] = {0,1,2}, preMirror[] = {0,2,1}
Output: {0, 1, 2}
Explanation: The tree will look like

šŸ‘ 2056958016

Input: pre[] = {1, 2, 4, 5, 3, 6, 7}, preMirror[] = {1, 3, 7, 6, 2, 5, 4}
Output: {1, 2, 4, 5, 3, 6, 7}
Explanation: The tree will look like

šŸ‘ construct-full-binary-tree-using-its-preorder-traversal-and-preorder-traversal-of-its-mirror-tree

[Naive Approach] Linear Search in Mirror Preorder – O(n²) Time and O(n) Space

We use pre[] to pick nodes in root order, and preMirror[] to determine how the tree is split into left and right subtrees.

  • Take the first element of pre[] as the root.
  • Maintain a variable preIndex to track the current position in pre[].
  • For the next element in pre[], find its position in preMirror[]. This position divides the current range into left subtree range and right subtree range. In the above example pre[] = {1, 2, 4, 5, 3, 6, 7}, preMirror[] = {1, 3, 7, 6, 2, 5, 4}. Using the position of 2 in preMirror[], we can find left and right subtrees.
  • Recursively construct the left subtree using the right part of the split range and the right subtree using the left part of the split range.

Output
4 2 5 1 6 3 7 

[Expected Approach] Hash Map or Dictionary Optimization – O(n) Time and O(n) Space

To optimize the above solution, we store the index of every element of preMirror[] in a Hash Map so that we can find the split position in O(1) time. This helps us quickly divide the current range into left and right subtrees and build the tree efficiently using recursion.

  • Store all elements of preMirror[] in a Hash Map with their indices for fast lookup.
  • Take the first element of pre[] as the root and maintain a pointer preIndex.
  • For the next element in pre[], use the Hash Map to directly find its index in preMirror[].
  • This index divides the current range into left subtree range and right subtree range.
  • Recursively construct the left subtree and right subtree using the same logic.

Let us consider: pre[] = {1, 2, 4, 5, 3, 6, 7} , preMirror[] = {1, 3, 7, 6, 2, 5, 4}

Step 1:

  • Root = 1 (first element of pre[])
  • Store indices of preMirror[] in HashMap: {1→0, 3→1, 7→2, 6→3, 2→4, 5→5, 4→6}

Step 2:

  • Next element in pre[] is 2
  • Using HashMap, index of 2 in preMirror[] = 4
  • This splits range into:
  • Left subtree range: [4 ... 6]
  • Right subtree range: [1 ... 3]

Step 3: Recursively build left and right subtrees using the same process.

The tree is successfully constructed, and its inorder traversal becomes: 4 2 5 1 6 3 7


Output
4 2 5 1 6 3 7 
Comment
Article Tags: