For Leetcode 105 I've got the O(N^2) solution working and I'm attempting to optimize to an O(N) solution but the output tree is the wrong structure.
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.
My original O(N^2) solution.
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
preorder_queue = deque(preorder)
def helper(preorder, inorder):
if not preorder or not inorder:
return None
root = TreeNode(preorder.popleft())
root_index = inorder.index(root.val)
root.left = helper(preorder, inorder[:root_index])
root.right = helper(preorder, inorder[root_index+1:])
return root
return helper(preorder_queue, inorder)
The slow operation is here is the inorder.index(root.val) since an index operation in a list is O(N). I'm trying to optimize this by storing a map of Node values -> indexes in my solution below.
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
preorder_queue = deque(preorder)
cache = {}
for i, node_val in enumerate(inorder):
cache[node_val] = i
def helper(preorder, inorder):
if not preorder or not inorder:
return None
root = TreeNode(preorder.popleft())
root_index = cache[root.val]
root.left = helper(preorder, inorder[:root_index])
root.right = helper(preorder, inorder[root_index+1:])
return root
return helper(preorder_queue, inorder)
However for the input preorder = [3,9,20,15,7] and inorder = [9,3,15,20,7] my output is giving me [3,9,20,null,null,15,null,7] instead of the correct tree structure [3,9,20,null,null,15,7]. Can anyone explain what's wrong with the cache solution?