Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions data_structures/binary_tree/top_view_of_binary_tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from collections import deque


# Definition for a binary tree node
class TreeNode:
def __init__(self, val=0, left=None, right=None):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: val

Please provide type hint for the parameter: left

Please provide type hint for the parameter: right

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: val

Please provide type hint for the parameter: left

Please provide type hint for the parameter: right

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: val

Please provide type hint for the parameter: left

Please provide type hint for the parameter: right

self.data = val
self.left = left
self.right = right


class Solution:
# Function to return the top view of the binary tree
def top_view(self, root):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: top_view. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file data_structures/binary_tree/top_view_of_binary_tree.py, please provide doctest for the function top_view

Please provide type hint for the parameter: root

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: top_view. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file data_structures/binary_tree/top_view_of_binary_tree.py, please provide doctest for the function top_view

Please provide type hint for the parameter: root

# List to store the result
ans = []

# Check if the tree is empty
if root is None:
return ans

# Dictionary to store the top view nodes based on their vertical positions
mpp = {}

# Queue for BFS traversal
q = deque([(root, 0)])

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

if line not in mpp:
mpp[line] = node.data

# Process left child
if node.left:
# Push the left child with a decreased vertical position into the queue
q.append((node.left, line - 1))

# Process right child
if node.right:
q.append((node.right, line + 1))

# Transfer values from the map to the result list
for key in sorted(mpp.keys()):
ans.append(mpp[key])

return ans


# Creating a sample binary tree
root = TreeNode(1)
root.left = TreeNode(2)
root.left.left = TreeNode(4)
root.left.right = TreeNode(10)
root.left.left.right = TreeNode(5)
root.left.left.right.right = TreeNode(6)
root.right = TreeNode(3)
root.right.right = TreeNode(10)
root.right.left = TreeNode(9)

solution = Solution()

# Get the top view traversal
top_view = solution.top_view(root)

# Print the result
print("Top View Traversal:")
for node in top_view:
print(node, end=" ")