From a8362a24d16a570382771ed44c8d7ffcf1cd1397 Mon Sep 17 00:00:00 2001 From: Yash Kadam <81897342+yashdkadam@users.noreply.github.com> Date: Sun, 5 Oct 2025 12:31:00 +0530 Subject: [PATCH 1/5] Adding top_view_of_binary_tree.py --- .../binary_tree/top_view_of_binary_tree.py | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 data_structures/binary_tree/top_view_of_binary_tree.py diff --git a/data_structures/binary_tree/top_view_of_binary_tree.py b/data_structures/binary_tree/top_view_of_binary_tree.py new file mode 100644 index 000000000000..12f276deb8a3 --- /dev/null +++ b/data_structures/binary_tree/top_view_of_binary_tree.py @@ -0,0 +1,70 @@ +from collections import deque + +# Definition for a binary tree node +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.data = val + self.left = left + self.right = right + +class Solution: + # Function to return the top view of the binary tree + def topView(self, 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, each element is a pair containing node and its vertical position + 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 the vertical position is not already in the map, add the node's data to the map + 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: + # Push the right child with an increased vertical position into the queue + 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.topView(root) + +# Print the result +print("Top View Traversal:") +for node in top_view: + print(node, end=" ") From 7c2dd04772840022a164764bc0c7148212c85dda Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 5 Oct 2025 07:04:25 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../binary_tree/top_view_of_binary_tree.py | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/data_structures/binary_tree/top_view_of_binary_tree.py b/data_structures/binary_tree/top_view_of_binary_tree.py index 12f276deb8a3..1c390513a856 100644 --- a/data_structures/binary_tree/top_view_of_binary_tree.py +++ b/data_structures/binary_tree/top_view_of_binary_tree.py @@ -1,5 +1,6 @@ from collections import deque + # Definition for a binary tree node class TreeNode: def __init__(self, val=0, left=None, right=None): @@ -7,47 +8,49 @@ def __init__(self, val=0, left=None, right=None): self.left = left self.right = right + class Solution: # Function to return the top view of the binary tree def topView(self, 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, each element is a pair containing node and its vertical position 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 the vertical position is not already in the map, add the node's data to the map 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: # Push the right child with an increased vertical position into the queue 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) From f84087517f3b69290cca5a7896b22cec03cc0914 Mon Sep 17 00:00:00 2001 From: Yash Kadam <81897342+yashdkadam@users.noreply.github.com> Date: Sun, 5 Oct 2025 13:11:08 +0530 Subject: [PATCH 3/5] Updated top_view_of_binary_tree.py with corrections --- data_structures/binary_tree/top_view_of_binary_tree.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/data_structures/binary_tree/top_view_of_binary_tree.py b/data_structures/binary_tree/top_view_of_binary_tree.py index 1c390513a856..85f1bb7b3d3d 100644 --- a/data_structures/binary_tree/top_view_of_binary_tree.py +++ b/data_structures/binary_tree/top_view_of_binary_tree.py @@ -11,7 +11,7 @@ def __init__(self, val=0, left=None, right=None): class Solution: # Function to return the top view of the binary tree - def topView(self, root): + def top_view(self, root): # List to store the result ans = [] @@ -22,7 +22,7 @@ def topView(self, root): # Dictionary to store the top view nodes based on their vertical positions mpp = {} - # Queue for BFS traversal, each element is a pair containing node and its vertical position + # Queue for BFS traversal q = deque([(root, 0)]) # BFS traversal @@ -30,7 +30,7 @@ def topView(self, root): # Retrieve the node and its vertical position from the front of the queue node, line = q.popleft() - # If the vertical position is not already in the map, add the node's data to the map + if line not in mpp: mpp[line] = node.data @@ -41,7 +41,7 @@ def topView(self, root): # Process right child if node.right: - # Push the right child with an increased vertical position into the queue + q.append((node.right, line + 1)) # Transfer values from the map to the result list From 2e8bf9a917fddad2e4c271ec2c03c10ee13845fe Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 5 Oct 2025 07:41:27 +0000 Subject: [PATCH 4/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/binary_tree/top_view_of_binary_tree.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/data_structures/binary_tree/top_view_of_binary_tree.py b/data_structures/binary_tree/top_view_of_binary_tree.py index 85f1bb7b3d3d..19c58ebdbcb5 100644 --- a/data_structures/binary_tree/top_view_of_binary_tree.py +++ b/data_structures/binary_tree/top_view_of_binary_tree.py @@ -30,7 +30,6 @@ def top_view(self, root): # 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 @@ -41,7 +40,6 @@ def top_view(self, root): # Process right child if node.right: - q.append((node.right, line + 1)) # Transfer values from the map to the result list From c48cef17b35b9f4e0520d19e0ca400ed23848c74 Mon Sep 17 00:00:00 2001 From: Yash Kadam <81897342+yashdkadam@users.noreply.github.com> Date: Sun, 5 Oct 2025 13:14:42 +0530 Subject: [PATCH 5/5] Update top_view_of_binary_tree.py --- data_structures/binary_tree/top_view_of_binary_tree.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/binary_tree/top_view_of_binary_tree.py b/data_structures/binary_tree/top_view_of_binary_tree.py index 19c58ebdbcb5..0719413cb23a 100644 --- a/data_structures/binary_tree/top_view_of_binary_tree.py +++ b/data_structures/binary_tree/top_view_of_binary_tree.py @@ -63,7 +63,7 @@ def top_view(self, root): solution = Solution() # Get the top view traversal -top_view = solution.topView(root) +top_view = solution.top_view(root) # Print the result print("Top View Traversal:")