-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbin.py
More file actions
52 lines (43 loc) · 1.17 KB
/
bin.py
File metadata and controls
52 lines (43 loc) · 1.17 KB
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
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def postorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
"""
#Recursive Solution
def traverse(root):
# Recursion
if not root:
return
else:
traverse(root.left)
traverse(root.right)
nodes.append(root.val)
nodes = []
traverse(root)
return nodes
"""
"""
# Hacky Solution - Iterative Solution
if not root:
return []
nodes = []
result = []
nodes.append(root)
while len(nodes) > 0:
current = nodes.pop()
result.append(current.val)
if current.left:
nodes.append(current.left)
if current.right:
nodes.append(current.right)
return result[::-1]
"""
# Proper Iterative