-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path145_binaryTreePostorderTraversal.py
45 lines (37 loc) · 1.41 KB
/
145_binaryTreePostorderTraversal.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
# 145. Binary Tree Postorder Traversal
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def postorderTraversal(self, root: TreeNode) -> List[int]:
# l = []
# if(root):
# l+=self.postorderTraversal(root.left)
# l+=self.postorderTraversal(root.right)
# l.append(root.val)
# return l
#---------------------------------------------------------------------------------
# 1. Push root to first stack.
# 2. Loop while first stack is not empty
# 2.1 Pop a node from first stack and push it to second stack
# 2.2 Push left and right children of the popped node to first stack
# 3. Print contents of second stack
nodeStack1 = []
nodeStack2 = []
nodeStack1.append(root)
# print(root)
if root==None:
return None
while(nodeStack1):
node = nodeStack1.pop()
nodeStack2.append(node.val)
if(node.left!=None):
nodeStack1.append(node.left)
if(node.right!=None):
nodeStack1.append(node.right)
# print(nodeStack1, nodeStack2)
nodeStack2.reverse()
return nodeStack2