VOOZH about

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

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


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 23 Jul, 2025

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

Approach:

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.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 

Time Complexity: O(n^3)
Auxiliary Space: O(n), where n is the number of nodes.

Related articles:

Comment
Article Tags: