Example
Input
preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output
A tree whose preorder traversal is [3,9,20,15,7]
Reconstruct the tree and verify preorder traversal matches the input preorder.
Full lesson preview
Reconstruct a binary tree given preorder and inorder traversal lists.
Problem statement
Task
Examples
Input
preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output
A tree whose preorder traversal is [3,9,20,15,7]
Reconstruct the tree and verify preorder traversal matches the input preorder.
Input format
Output format
Constraints
Samples
Input
preorder = [1], inorder = [1]
Output
Tree with a single node 1 (preorder traversal [1])
Trivial single-node tree reconstruction.