Example 1
Input
inorder = [9, 3, 15, 20, 7], postorder = [9, 15, 7, 20, 3]
Output
[3, 9, 20, 15, 7]
The reconstructed tree has root 3, left child 9, and right subtree with root 20 and children 15 and 7. Preorder (root-left-right) is [3,9,20,15,7].
Full lesson preview
Reconstruct a binary tree from its inorder and postorder traversals and return its preorder traversal.
Problem statement
Task
Examples
Input
inorder = [9, 3, 15, 20, 7], postorder = [9, 15, 7, 20, 3]
Output
[3, 9, 20, 15, 7]
The reconstructed tree has root 3, left child 9, and right subtree with root 20 and children 15 and 7. Preorder (root-left-right) is [3,9,20,15,7].
Input format
Output format
Constraints
Samples
Input
inorder = [2, 1, 3], postorder = [2, 3, 1]
Output
[1, 2, 3]
Reconstructed tree root 1 with left 2 and right 3 -> preorder [1,2,3].