Skip to content

Commit b214d55

Browse files
author
Chris Wu
committed
no message
1 parent 6c8d6cb commit b214d55

5 files changed

+110
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from collections import deque
2+
3+
class Solution(object):
4+
def levelOrderBottom(self, root):
5+
opt = []
6+
7+
if not root: return opt
8+
9+
q = deque()
10+
q.append((root, 0))
11+
12+
while q:
13+
node, depth = q.popleft()
14+
if depth<len(opt):
15+
opt[depth].append(node.val)
16+
else:
17+
opt.append([node.val])
18+
if node.left: q.append((node.left, depth+1))
19+
if node.right: q.append((node.right, depth+1))
20+
return reversed(opt)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from collections import deque
2+
3+
class Solution(object):
4+
def levelOrder(self, root):
5+
opt = []
6+
7+
if not root: return opt
8+
9+
q = deque()
10+
q.append((root, 0))
11+
12+
while q:
13+
node, depth = q.popleft()
14+
if depth<len(opt):
15+
opt[depth].append(node.val)
16+
else:
17+
opt.append([node.val])
18+
if node.left: q.append((node.left, depth+1))
19+
if node.right: q.append((node.right, depth+1))
20+
return opt
21+
22+
"""
23+
Time complexity is O(N). Because we perform a BFS, where N is the number of nodes. (We traverse all the nodes in the tree)
24+
Time complexity is O(N), too. Because we might potentially store all the nodes in the queue.
25+
"""
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from collections import deque
2+
3+
class Solution(object):
4+
def levelOrder(self, root):
5+
opt = []
6+
7+
if not root: return opt
8+
9+
q = deque()
10+
q.append((root, 0))
11+
12+
while q:
13+
node, depth = q.popleft()
14+
if depth<len(opt):
15+
opt[depth].append(node.val)
16+
else:
17+
opt.append([node.val])
18+
if node.children:
19+
for child in node.children:
20+
q.append((child, depth+1))
21+
return opt
22+
23+
"""
24+
Time complexity is O(N). Because we perform a BFS, where N is the number of nodes. (We traverse all the nodes in the tree)
25+
Time complexity is O(N), too. Because we might potentially store all the nodes in the queue.
26+
"""

problems/univalued-binary-tree.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution(object):
2+
def isUnivalTree(self, root):
3+
stack = []
4+
stack.append(root)
5+
value = root.val
6+
while stack:
7+
node = stack.pop()
8+
if node.val!=value: return False
9+
if node.left: stack.append(node.left)
10+
if node.right: stack.append(node.right)
11+
return True
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
The description wants us to report from left to right, top to bottom.
3+
Thus, we keep add value into `temp` with `x_position` as the key and `(y_position, node.val)` as the value.
4+
And maintain `min_index`, `max_index` at the same time. So we know how to iterate the `temp`.
5+
"""
6+
from collections import defaultdict
7+
8+
class Solution(object):
9+
def verticalTraversal(self, root):
10+
temp = defaultdict(list)
11+
min_index = 0
12+
max_index = 0
13+
14+
stack = []
15+
stack.append((root, 0, 0))
16+
while stack:
17+
node, x, y = stack.pop()
18+
temp[x].append((y, node.val)) #append the value of height, so we can sort by height later on
19+
min_index = min(min_index, x)
20+
max_index = max(max_index, x)
21+
if node.left: stack.append((node.left, x-1, y+1))
22+
if node.right: stack.append((node.right, x+1, y+1))
23+
24+
opt = []
25+
for i in range(min_index, max_index+1):
26+
opt.append([v for y, v in sorted(temp[i])]) #the temp[i] will be sorted by y_position then sorted by node.val
27+
28+
return opt

0 commit comments

Comments
 (0)