55. Construct Binary Tree from Preorder and Inorder

Medium · Binary Tree

Given two arrays representing the preorder and inorder traversals of a binary tree, reconstruct the tree and return it as a level-order array.

The input is a list [preorder, inorder] where: - preorder is an array of node values in preorder traversal order (root, left subtree, right subtree) - inorder is an array of node values in inorder traversal order (left subtree, root, right subtree)

Return the reconstructed binary tree as a level-order (breadth-first) array, using null to represent missing children. Do not include trailing nulls in your output (matching standard LeetCode serialisation).

Examples

Example 1
Input: [[3,9,20,15,7],[9,3,15,20,7]]
Output: [3,9,20,null,null,15,7]
Explanation: Standard textbook example
Example 2
Input: [[-1],[-1]]
Output: [-1]
Explanation: Single node tree

Constraints