From bbdd47ffd3f931e6d5b61150e1c9de520d184d86 Mon Sep 17 00:00:00 2001 From: bt3gl <138340846+bt3gl-cryptographer@users.noreply.github.com> Date: Tue, 8 Aug 2023 19:51:19 -0700 Subject: [PATCH] Update bt_construct_inorder_postorder.py --- trees/bt_construct_inorder_postorder.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/trees/bt_construct_inorder_postorder.py b/trees/bt_construct_inorder_postorder.py index 697186d..59031b3 100644 --- a/trees/bt_construct_inorder_postorder.py +++ b/trees/bt_construct_inorder_postorder.py @@ -3,7 +3,7 @@ # author: bt3gl -def build_tree(left, right, index_map): +def build_tree(left, right, index_map, postorder): if left > right: return None @@ -11,15 +11,15 @@ def build_tree(left, right, index_map): root = Node(postorder.pop()) # this order change from preorder index_here = index_map[root.val] - root.right = build_tree(index_here + 1, right, index_map) # this order change from preorder - root.left = build_tree(left, index_here - 1, index_map) + 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, postorder) return root -def build_tree(inorder, postorder) -> Optional[Node]: +def build_tree(inorder, postorder): 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)