-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path145.py
More file actions
28 lines (24 loc) · 803 Bytes
/
145.py
File metadata and controls
28 lines (24 loc) · 803 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
from common import TreeNode
class Solution:
def postorderTraversal(self, root):
if not root:
return []
res, visited = [], set()
stack = [root]
node = root
while len(stack):
if node.left and node.left not in visited:
stack.append(node)
node = node.left
elif node.right and node.right not in visited:
stack.append(node)
node = node.right
else:
res.append(node.val)
visited.add(node)
node = stack.pop()
return res
if __name__ == '__main__':
solution = Solution()
print(TreeNode.list2Tree([1, None, 2, 3]))
print(solution.postorderTraversal(TreeNode.list2Tree([1, None, 2, 3])))