Rebuild a tree from its traversals
Write rebuild(preorder, inorder): the two lists are walks of the same binary tree, whose values are all distinct. preorder lists each node before its left subtree and then its right subtree; inorder lists the left subtree, then the node, then the right subtree. Rebuild that tree and return its root; two empty lists mean an empty tree. Each TreeNode has val, left and right.
The tests call the provided run_rebuild(preorder, inorder), which returns your tree level by level with null for a missing child: [1, null, 2, 3] is a root 1 with only a right child 2, whose left child is 3.
Aim for O(n).
Examples
run_rebuild([3,9,20,15,7], [9,3,15,20,7])→[3,9,20,null,null,15,7]run_rebuild([1,2,3], [3,2,1])→[1,2,null,3]run_rebuild([], [])→[]