Rebuild a tree from two traversals
Write build(preorder, inorder) that returns the root of the binary tree whose pre-order
and in-order traversals are the two given lists. Values are distinct. Import TreeNode
from checker.
build([3, 9, 20, 15, 7], [9, 3, 15, 20, 7]) -> the tree [3, 9, 20, None, None, 15, 7]
build([], []) -> None
The first pre-order value is the root; its position in the in-order list splits the rest into left and right subtrees. A dictionary from value to in-order index keeps it linear.
def build(preorder, inorder):
...