Construct Binary Tree from Preorder and Inorder Traversal
MediumP2Coding
Given the preorder and inorder traversals of a binary tree with unique values, rebuild the tree and return its root.
Example 1
- Input
- preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
- Output
- [3,9,20,null,null,15,7]
Example 2
- Input
- preorder = [-1], inorder = [-1]
- Output
- [-1]
Constraints
- 1 <= preorder.length <= 3000
- inorder.length == preorder.length
- -3000 <= values <= 3000
- All values are unique and both arrays describe the same tree.
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 9. ⌘Enter runs, ⌘⇧Enter submits.