Prepin
Log in

Build a binary tree based on its preorder and inorder traversal sequences.

Approach / Explanation

This inquiry revolves around constructing a binary tree using the provided pre-order and in-order traversal data. A binary tree is a type of tree data structure where each node can have a maximum of two children, known as the left child and the right child. In the realm of tree data structures, a traversal refers to the method of visiting all the nodes in a specific sequence. Below is a concise overview of both types of traversal: Preorder Traversal : In this method, the root node is accessed first, followed by the left subtree, and then the right subtree. The recursion follows this pattern: Preorder(root.leftChild) and Preorder(root.rightChild). Inorder Traversal : This method involves visiting the left subtree first, then the root, and finally the right subtree. The recursion pattern is: Inorder(root.leftChild), root, Inorder(root.rightChild). Your task is to reconstruct the original binary tree using these two traversal sequences. To effectively address this question, it is essential to grasp how these traversals function and their significance in rebuilding the tree: The first element in the pre-order traversal indicates the root of the tree. The in-order traversal helps in identifying the separation between the left and right sub-trees.

Suggested Answer

This is a general method for tackling and resolving this issue. The following example is written in Python. # Python function for creating a binary tree from two traversal sequences. class Node : def __init__ ( self , data ) : self . data = data self . left = None self . right = None def buildTree ( inOrder , preOrder , inStrt , inEnd ) : if ( inStrt > inEnd ) : return None # Select the current node from the Preorder sequence with preIndex and increase preIndex tNode = Node ( preOrder [ buildTree . preIndex ] ) buildTree . preIndex += 1 # If this node has no children, return it if inStrt == inEnd : return tNode # Otherwise, find the index of this node in the Inorder sequence inIndex = search ( inOrder , inStrt , inEnd , tNode . data ) # Using the index from the Inorder sequence, build the left and right subtrees tNode . left = buildTree ( inOrder , preOrder , inStrt , inIndex - 1 ) tNode . right = buildTree ( inOrder , preOrder , inIndex + 1 , inEnd ) return tNode # Function to find and return the index def search ( arr , strt , end , value ) : for i in range ( strt , end + 1 ) : if arr [ i ] == value : return i buildTree . preIndex = 0

Alternative Answer

Here’s another C++ example that illustrates a different approach to solving the problem. // C++ program to construct binary tree from given inorder and preorder traversal # include <bits/stdc++.h> using namespace std ; struct Node { char data ; struct Node * left ; struct Node * right ; } ; struct Node * newNode ( char data ) { struct Node * node = new Node ( ) ; node -> data = data ; node -> left = NULL ; node -> right = NULL ; return ( node ) ; } struct Node * buildTree ( char in [ ] , char pre [ ] , int inStrt , int inEnd ) { static int preIndex = 0 ; if ( inStrt > inEnd ) return NULL ; Node * tNode = newNode ( pre [ preIndex ++ ] ) ; if ( inStrt == inEnd ) return tNode ; int inIndex = search ( in , inStrt , inEnd , tNode -> data ) ; tNode -> left = buildTree ( in , pre , inStrt , inIndex - 1 ) ; tNode -> right = buildTree ( in , pre , inIndex + 1 , inEnd ) ; return tNode ; } int search ( char arr [ ] , int strt , int end , char value ) { int i ; for ( i = strt ; i <= end ; i ++ ) { if ( arr [ i ] == value ) return i ; } } These examples demonstrate the use of in-place map searching to determine the left and right subtrees, enhancing the overall efficiency of the solution.

Question Details

Difficulty & Category

Medium
algorithms

Companies

Amazon

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.