Skip to content

Commit f840875

Browse files
authored
Updated top_view_of_binary_tree.py with corrections
1 parent 7c2dd04 commit f840875

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

data_structures/binary_tree/top_view_of_binary_tree.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def __init__(self, val=0, left=None, right=None):
1111

1212
class Solution:
1313
# Function to return the top view of the binary tree
14-
def topView(self, root):
14+
def top_view(self, root):
1515
# List to store the result
1616
ans = []
1717

@@ -22,15 +22,15 @@ def topView(self, root):
2222
# Dictionary to store the top view nodes based on their vertical positions
2323
mpp = {}
2424

25-
# Queue for BFS traversal, each element is a pair containing node and its vertical position
25+
# Queue for BFS traversal
2626
q = deque([(root, 0)])
2727

2828
# BFS traversal
2929
while q:
3030
# Retrieve the node and its vertical position from the front of the queue
3131
node, line = q.popleft()
3232

33-
# If the vertical position is not already in the map, add the node's data to the map
33+
3434
if line not in mpp:
3535
mpp[line] = node.data
3636

@@ -41,7 +41,7 @@ def topView(self, root):
4141

4242
# Process right child
4343
if node.right:
44-
# Push the right child with an increased vertical position into the queue
44+
4545
q.append((node.right, line + 1))
4646

4747
# Transfer values from the map to the result list

0 commit comments

Comments
 (0)