Given two integer arrays preorder
and inorder
representing traversals of a binary tree:
preorder
contains the preorder traversal (root, left, right)inorder
contains the inorder traversal (left, root, right)Both arrays:
Construct and return the original binary tree.
Constraints:
Examples:
// Example 1: // 1 // / \ // 2 3 // \ // 4 const preorder1 = [1, 2, 3, 4]; const inorder1 = [2, 1, 3, 4]; console.log(buildTree(preorder1, inorder1)); // Output: [1, 2, 3, null, null, null, 4] // Example 2: // 1 const preorder2 = [1]; const inorder2 = [1]; console.log(buildTree(preorder2, inorder2)); // Output: [1]