mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-30 12:46:11 -04:00
Update construct_tree_inorder_postorder.py
This commit is contained in:
parent
0526bb7aee
commit
38cbe1c55b
@ -1,10 +1,12 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# author: bt3gl
|
||||||
# Given two integer arrays inorder and postorder where inorder is the inorder
|
# Given two integer arrays inorder and postorder where inorder is the inorder
|
||||||
# traversal of a binary tree and postorder is the postorder traversal of the
|
# traversal of a binary tree and postorder is the postorder traversal of the
|
||||||
# same tree, construct and return the binary tree.
|
# same tree, construct and return the binary tree.
|
||||||
|
|
||||||
def build_tree(inorder: list[int], postorder: list[int]) -> Optional[TreeNode]:
|
|
||||||
|
|
||||||
def fill_tree(i_left, i_right, inorder_map):
|
def fill_tree(i_left, i_right, inorder_map):
|
||||||
|
|
||||||
if i_left > i_right:
|
if i_left > i_right:
|
||||||
return None
|
return None
|
||||||
@ -19,7 +21,9 @@ def build_tree(inorder: list[int], postorder: list[int]) -> Optional[TreeNode]:
|
|||||||
|
|
||||||
return root
|
return root
|
||||||
|
|
||||||
inorder_map = {val: index for index, val in enumerate(inorder)}
|
|
||||||
|
|
||||||
|
def build_tree(inorder: list[int], postorder: list[int]) -> Optional[TreeNode]:
|
||||||
|
|
||||||
|
inorder_map = {val: index for index, val in enumerate(inorder)}
|
||||||
return fill_tree(0, len(inorder) - 1, inorder_map)
|
return fill_tree(0, len(inorder) - 1, inorder_map)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user