-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary Tree Postorder Traversal.py
More file actions
38 lines (34 loc) · 1021 Bytes
/
Binary Tree Postorder Traversal.py
File metadata and controls
38 lines (34 loc) · 1021 Bytes
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
# Definition for a binary tree node
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
# @param root, a tree node
# @return a list of integers
def postorderTraversal(self, root):
stack = []
result = []
current = root
previous = None
while current is not None or len(stack) > 0:
while current is not None:
stack.append(current)
current = current.left
current = stack[-1]
if current.right is None or current.right is previous:
result.append(current.val)
stack.pop()
previous = current
current = None
else:
current = current.right
return result
p = TreeNode(1)
p.left = TreeNode(2)
p.right = TreeNode(3)
p.right.left = TreeNode(4)
p.right.left.right = TreeNode(5)
result = Solution()
print result.postorderTraversal(p)