Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree.
Example 1
Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output: [3,9,20,null,null,15,7]
Explanation: The first preorder value, 3, is the root. In inorder, [9] is to its left and [15,20,7] to its right. The next preorder value, 20, is the right subtree's root, with 15 on its left and 7 on its right.
Example 2
Input: preorder = [-1], inorder = [-1]
Output: [-1]
Explanation: Both traversals contain only -1, so the tree is that single node.
Example 3
Input: preorder = [1,2,3], inorder = [3,2,1]
Output: [1,2,null,3]
Explanation: Preorder says 1 is the root. In inorder, 3 and 2 both come before 1, so they are in the left subtree. Preorder then gives 2 as that subtree's root, and 3 comes before 2 in inorder, so 3 is 2's left child.
The first value of preorder is always the root. Find that root inside inorder: everything to its left is the left subtree, everything to its right is the right subtree. Recurse, consuming preorder values in order.
Run checks all cases above. Submit evaluates all test cases.