Data Structures19 sections · 729 units
Open in Course

Problem - Construct from Traversals

Build tree from sequences

Given two integer arrays preorder and inorder where preorder is the preorder traversal and inorder is the inorder traversal of the same tree, construct and return the binary tree.

Example: preorder =[3,9,20,15,7]= [3,9,20,15,7], inorder =[9,3,15,20,7]= [9,3,15,20,7] produces tree [3,9,20,null,null,15,7][3,9,20,null,null,15,7].

This problem tests your understanding of traversal orders and recursive construction. Constraints: arrays have length 11 to 30003000, values from 3000-3000 to 30003000, all values unique.