Search⌘ K
AI Features

Problem:Construct Binary Tree from Preorder and Inorder Traversal

Understand how to reconstruct a binary tree from given preorder and inorder traversal arrays. Learn to implement a recursive solution that uses a hashmap for fast index lookup, and grasp the time and space complexity of this approach. This lesson helps you build a foundational skill in tree manipulation and recursion in JavaScript.

Statement

You are given two integer arrays, preorder and inorder, representing the preorder traversal and inorder traversal of the same binary tree, respectively. Your task is to reconstruct and return the binary tree from these two traversal sequences.

Note: All values in the tree are unique, and every value present in inorder is also present in preorder.

Constraints:

  • 11 \leq preorder.length 3000\leq 3000

  • inorder.length == preorder.length

  • 3000-3000 \leq preorder[i], inorder[i] 3000\leq 3000 ...