Update bt_construct_inorder_preorder.py

This commit is contained in:
bt3gl 2023-08-08 14:47:15 -07:00 committed by GitHub
parent e5efd5424a
commit dd98393aa4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,11 +10,10 @@ def build_tree(preorder, inorder) -> Optional[Node]:
if left > right: if left > right:
return None return None
root = Node(preorder.pop(0)) root = Node(preorder.pop(0)) # # this order change from postorder
index_here = index_map[root.val] index_here = index_map[root.val]
# this order change from postorder root.left = helper(left, index_here - 1, index_map) # this order change from postorder
root.left = helper(left, index_here - 1, index_map)
root.right = helper(index_here + 1, right, index_map) root.right = helper(index_here + 1, right, index_map)
return root return root