Skip to content

Commit 4d4eaad

Browse files
committed
updated bottom and top view of binary tree solution
1 parent 73d09e6 commit 4d4eaad

2 files changed

Lines changed: 21 additions & 28 deletions

File tree

leetcode/src/binary_trees/bottom_view_of_binary_tree/bottom_view.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,35 @@ def __init__(self, val):
44
self.val = val
55
self.left = self.right = None
66

7-
# Time ( O(N) + O(WLog (W) * KLog(k)) )
7+
# Time O(N) + O(WLog W)
88
# Where N is the number of nodes in the tree
99
# W is the the width of the tree
10-
# K is the average length of nodes in the height of the tree
1110

12-
# Space O(n) + O(n) -> O(N)
13-
# Where N is the number of nodes in the tree
11+
# Space O(N/2) + O(W) --> O(N) + O(W)
12+
# Where N is the number of nodes on a particular level
13+
# Where W is the number of nodes spread across the with of the tree if we consider the results array
1414

1515
def bottomView(root):
1616
if not root: return
1717

18-
vertical_order = defaultdict(dict)
19-
queue = deque([(root, 0, 0)])
18+
vertical_order = {}
19+
queue = deque([(root, 0)])
2020

2121
while queue:
2222
level_size = len(queue)
2323
for _ in range(level_size):
24-
node, x, y = queue.popleft()
24+
node, x = queue.popleft()
2525

26-
if x not in vertical_order:
27-
vertical_order[x] = defaultdict(list)
28-
vertical_order[x][y].append(node.val)
26+
vertical_order[x] = node.val
2927

3028
if node.left:
31-
queue.append((node.left, x-1, y+1))
29+
queue.append((node.left, x-1))
3230
if node.right:
33-
queue.append((node.right, x+1, y+1))
31+
queue.append((node.right, x+1))
3432

3533
result = []
3634
for key in sorted(vertical_order.keys()):
37-
val_keys = sorted(vertical_order[key].keys())
38-
bottom_view_key = val_keys[-1]
39-
result.append(vertical_order[key][bottom_view_key].pop())
35+
result.append(vertical_order[key])
4036

4137
return result
4238

leetcode/src/binary_trees/top_view_of_binary_tree/top_view.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,37 @@ def __init__(self, val):
77

88

99

10-
# Time ( O(N) + O(WLog (W) * KLog(k)) )
10+
# Time O(N) + O(WLog W)
1111
# Where N is the number of nodes in the tree
1212
# W is the the width of the tree
13-
# K is the average length of nodes in the height of the tree
1413

15-
# Space O(n) + O(n) -> O(N)
16-
# Where N is the number of nodes in the tree
14+
# Space O(N/2) + O(W) --> O(N) + O(W)
15+
# Where N is the number of nodes on a particular level
16+
# Where W is the number of nodes spread across the with of the tree if we consider the results array
1717

1818

1919
def topView(root):
2020
if not root: return
2121

2222
vertical_order = defaultdict(dict)
23-
queue = deque([(root, 0, 0)])
23+
queue = deque([(root, 0)])
2424

2525
while queue:
2626
level_size = len(queue)
2727
for _ in range(level_size):
28-
node, x, y = queue.popleft()
28+
node, x = queue.popleft()
2929

3030
if x not in vertical_order:
31-
vertical_order[x] = defaultdict(list)
32-
vertical_order[x][y].append(node.val)
31+
vertical_order[x] = node.val
3332

3433
if node.left:
35-
queue.append((node.left, x-1, y+1))
34+
queue.append((node.left, x-1))
3635
if node.right:
37-
queue.append((node.right, x+1, y+1))
36+
queue.append((node.right, x+1))
3837

3938
result = []
4039
for key in sorted(vertical_order.keys()):
41-
val_keys = sorted(vertical_order[key].keys())
42-
bottom_view_key = val_keys[0]
43-
result.append(vertical_order[key][bottom_view_key].pop())
40+
result.append(vertical_order[key])
4441

4542
return result
4643

0 commit comments

Comments
 (0)