-
-
Notifications
You must be signed in to change notification settings - Fork 48.6k
Adding top view of Binary Tree #13206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
a8362a2
84ef8d0
7c2dd04
f840875
2e8bf9a
c48cef1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: |
||
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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: |
||
# 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=" ") |
There was a problem hiding this comment.
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