forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_048.py
44 lines (36 loc) · 1.23 KB
/
problem_048.py
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
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def get_tree(preorder, inorder):
if not preorder or not inorder:
return None
root_char = preorder[0]
if len(preorder) == 1:
return Node(root_char)
root_node = Node(root_char)
for i, char in enumerate(inorder):
if char == root_char:
root_node.left = get_tree(
preorder=preorder[1:i+1], inorder=inorder[:i])
root_node.right = get_tree(
preorder=preorder[i+1:], inorder=inorder[i+1:])
return root_node
tree = get_tree(preorder=['a', 'b', 'd', 'e', 'c', 'f', 'g'],
inorder=['d', 'b', 'e', 'a', 'f', 'c', 'g'])
assert tree.val == 'a'
assert tree.left.val == 'b'
assert tree.left.left.val == 'd'
assert tree.left.right.val == 'e'
assert tree.right.val == 'c'
assert tree.right.left.val == 'f'
assert tree.right.right.val == 'g'
tree = get_tree(preorder=['a', 'b', 'd', 'e', 'c', 'g'],
inorder=['d', 'b', 'e', 'a', 'c', 'g'])
assert tree.val == 'a'
assert tree.left.val == 'b'
assert tree.left.left.val == 'd'
assert tree.left.right.val == 'e'
assert tree.right.val == 'c'
assert tree.right.right.val == 'g'