VOOZH about

URL: https://www.geeksforgeeks.org/dsa/construct-tree-inorder-level-order-traversals-set-2/

⇱ Construct a tree from Inorder and Level order traversals | Set 2 - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Construct a tree from Inorder and Level order traversals | Set 2

Last Updated : 9 Oct, 2024

Given in-order and level-order traversals of a Binary Tree, the task is to construct the Binary Tree and return its root.

Example: 

Input:
in[] = {4, 8, 10, 12, 14, 20, 22};
level[] = {20, 8, 22, 4, 12, 10, 14};
Output:

👁 Construct-a-tree-from-Inorder-and-Level-order-traversals

[Naive Approach] Using Recursion and hash map - O(n^2) Time and O(n) Space

The idea is to map the values of the in-order array to their respective indices in a hash map and construct the root node from the first element of the level order array. Find the index of this element in the in-order array using the hash map. Recursively create the left subtree from the elements present on the left side to the current element in the in-order array. Similarly, create the right subtree from the elements present on the right side to the current element in the in-order array.

Below is the implementation of the above approach: 


Output
4 8 10 12 14 20 22 

[Alternate Approach] Using Recursion and Hast Set - O(n^2) Time and O(n) Space

The idea is to construct the root node from the first element of the level order array. Find the index of this element in the in-order array. Then, find the elements in level order array which will be present in left subtree and right subtree using a hashset (Insert the left subtree elements from in-order array in set, then traverse the level-order array and check if the element will be present in left subtree or right subtree). Recursively create the left subtree and right subtree from the elements present on the left and right side to the current element in the in-order array.

Below is the implementation of the above approach:


Output
4 8 10 12 14 20 22 

[Expected Approach] Using Queue and Hash map- O(n) Time and O(n) Space

The idea is to use the first element in level order array to create the root node. Find the index of the same element in the in-order array. If elements are present on the left side, then create root->left node. Similarly, if elements are present on the right side, then create root->right node.

Step by step implementation:

  • As the first level of tree only consists of root node, so use the first element in the level array to create the root node.
  • Push this root node into a queue with some other information, s: It denotes the start index of the current segment in in-order traversal while e denotes the last index of the current segment in in-order traversal.
  • While q is not empty , follow below
    • Pop the the front node of the queue. Find the index of this node (currIndex) in the in-order traversal.
    • If s < currIndex, it means there is at least one element in left subtree of this node. So create node->left = level[index] and increment index. Also push {node->left, s, currIndex-1} into the queue.
    • if currIndex < e, then it means that there is at least one element in right subtree of this node. So create node->right= level[index] and increment index. Also push {node->right, currIndex+1, e} into the queue.

Below is the implementation of the above approach:


Output
4 8 10 12 14 20 22 

Related articles:

Comment