# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode:
table = {}
for idx, val in enumerate(inorder):
table[val] = idx
preorder = deque(preorder)
def traverse(leftIndex, rightIndex):
if leftIndex > rightIndex:
return None
root = TreeNode(preorder.popleft())
rootIndex = table[root.val]
root.left = traverse(leftIndex, rootIndex - 1)
root.right = traverse(rootIndex + 1, rightIndex)
return root
return traverse(0, len(inorder) - 1)