mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Update bt_construct_inorder_postorder.py
This commit is contained in:
parent
b9318bf6ea
commit
bbdd47ffd3
@ -3,7 +3,7 @@
|
|||||||
# author: bt3gl
|
# author: bt3gl
|
||||||
|
|
||||||
|
|
||||||
def build_tree(left, right, index_map):
|
def build_tree(left, right, index_map, postorder):
|
||||||
|
|
||||||
if left > right:
|
if left > right:
|
||||||
return None
|
return None
|
||||||
@ -11,15 +11,15 @@ def build_tree(left, right, index_map):
|
|||||||
root = Node(postorder.pop()) # this order change from preorder
|
root = Node(postorder.pop()) # this order change from preorder
|
||||||
index_here = index_map[root.val]
|
index_here = index_map[root.val]
|
||||||
|
|
||||||
root.right = build_tree(index_here + 1, right, index_map) # this order change from preorder
|
root.right = build_tree(index_here + 1, right, index_map, postorder) # this order change from preorder
|
||||||
root.left = build_tree(left, index_here - 1, index_map)
|
root.left = build_tree(left, index_here - 1, index_map, postorder)
|
||||||
|
|
||||||
return root
|
return root
|
||||||
|
|
||||||
|
|
||||||
def build_tree(inorder, postorder) -> Optional[Node]:
|
def build_tree(inorder, postorder):
|
||||||
|
|
||||||
index_map = {val: i for i, value in enumerate(inorder)}
|
index_map = {val: i for i, value in enumerate(inorder)}
|
||||||
|
|
||||||
return fill_tree(0, len(inorder) - 1, index_map)
|
return fill_tree(0, len(inorder) - 1, index_map, postorder)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user