-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path145.py
29 lines (28 loc) · 854 Bytes
/
145.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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# @param {TreeNode} root
# @return {integer[]}
def postorderTraversal(self, root):
if root == None:
return []
stack = [root]
traversal = []
visit = []
while len(stack) != 0:
root = stack[0]
if root.left != None:
if root.left not in visit:
stack.insert(0,root.left)
continue
if root.right != None:
if root.right not in visit:
stack.insert(0,root.right)
continue
visit.append(stack.pop(0))
traversal.append(root.val)
return traversal