forked from ByteByteGoHq/coding-interview-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_binary_tree.py
More file actions
46 lines (41 loc) · 1.42 KB
/
build_binary_tree.py
File metadata and controls
46 lines (41 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from ds import TreeNode
from typing import List
"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
"""
preorder_index = 0
inorder_indexes_map = {}
def build_binary_tree(preorder: List[int], inorder: List[int]) -> TreeNode:
global inorder_indexes_map
# Populate the hash map with the inorder values and their indexes.
for i, val in enumerate(inorder):
inorder_indexes_map[val] = i
# Build the tree and return its root node.
return build_subtree(0, len(inorder) - 1, preorder, inorder)
def build_subtree(left: int, right: int, preorder: List[int], inorder: List[int]) -> TreeNode:
global preorder_index, inorder_indexes_map
# Base case: if no elements are in this range, return None.
if left > right:
return None
val = preorder[preorder_index]
# Set 'inorder_index' to the index of the same value pointed at by
# 'preorder_index'.
inorder_index = inorder_indexes_map[val]
node = TreeNode(val)
# Advance 'preorder_index' so it points to the value of the next
# node to be created.
preorder_index += 1
# Build the left and right subtrees and connect them to the current
# node.
node.left = build_subtree(
left, inorder_index - 1, preorder, inorder
)
node.right = build_subtree(
inorder_index + 1, right, preorder, inorder
)
return node