Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

122. Construct Binary Tree from Preorder and Inorder Traversal

MediumDay 36 of 60Trees & BST

You are given two integer arrays: preorder, the preorder traversal of a binary tree, and inorder, the inorder traversal of the same tree. All values are unique. 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
  • All values are unique and both arrays describe the same tree
Hints
Frequently asked at: Amazon, Microsoft, Bloomberg, Adobe
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min