-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinary-tree-postorder-traversal.py
77 lines (60 loc) · 1.76 KB
/
binary-tree-postorder-traversal.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/python3
# https://leetcode.com/problems/binary-tree-postorder-traversal/
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def create_binary_tree(array):
root = TreeNode(array[0])
queue = [(0, root)]
while len(queue) > 0:
(i, p) = queue.pop(0)
li = i + 1
ri = i + 2
if li < len(array) and array[li] is not None:
t = TreeNode(array[li])
p.left = t
queue.append((li, t))
if ri < len(array) and array[ri] is not None:
t = TreeNode(array[ri])
p.right = t
queue.append((ri, t))
return root
def postorder_traversal_recursive(root):
if root:
postorder_traversal_recursive(root.left)
postorder_traversal_recursive(root.right)
print(root.val)
def peek(stack):
if len(stack) > 0:
return stack[-1]
return None
# using 2 stack
def postorder_traversal_iterative(root):
if root is None:
return
stack = []
ans = []
while True:
while root:
if root.right is not None:
stack.append(root.right)
stack.append(root)
root = root.left
root = stack.pop()
if root.right is not None and peek(stack) == root.right:
stack.pop()
stack.append(root)
root = root.right
else:
ans.append(root.val)
root = None
if len(stack) <= 0:
break
return ans
if __name__ == "__main__":
tree_array = [1, None, 2, 3]
root_node = create_binary_tree(tree_array)
# postorder_traversal_recursive(root_node)
print(postorder_traversal_iterative(root_node))