Skip to content

889. Construct Binary Tree from Preorder and Postorder Traversal #1350

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to construct a binary tree from its preorder and postorder traversals. The key challenge is to correctly identify the left and right subtrees from these traversals and recursively build the tree.

Approach

  1. Identify the Root: The root of the tree is the first element in the preorder traversal.
  2. Base Case: If the tree has only one node (root), return it directly.
  3. Left Subtree Identification: The left child of the root is the second element in the preorder traversal. Find the position of this left child in the postorder traversal to determine the boundary between the left and right subtrees.
  4. Split Traversals: Using the position of the left child in the postorder traversal, split both …

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim Feb 23, 2025
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants