
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program to find top view of a binary tree in Python
Suppose we have a binary tree, we have to find the top view of the tree, they will be sorted left−to−right.
So, if the input is like image, then the output will be [3, 5, 8, 6, 9], as 3 is above 2 and 5 is above 7 so they are not visible.
To solve this, we will follow these steps −
view := a new empty map
q := a double ended queue
insert pair (root, 0) at the end of q
start := inf, end := −inf
while q is not empty, do
(node, coord) := left element of q, then remove left element of q
start := minimum of start and coord
end := maximum of end and coord
if coord is not in view, then
view[coord] := value of node
if left of node is not null, then
insert (left of node, coord − 1) at the end of q
if right of node is not null, then
insert (right of node, coord + 1) at the end of q
res := a new list
for i in range start to end, do
if i is in view, then
insert view[i] at the end of res
return res
Let us see the following implementation to get better understanding −
Example
from collections import deque class TreeNode: def __init__(self, data, left = None, right = None): self.val = data self.left = left self.right = right class Solution: def solve(self, root): view = {} q = deque() q.append((root, 0)) start = float("inf") end = float("-inf") while q: node, coord = q.popleft() start = min(start, coord) end = max(end, coord) if coord not in view: view[coord] = node.val if node.left: q.append((node.left, coord - 1)) if node.right: q.append((node.right, coord + 1)) res = [] for i in range(start, end + 1): if i in view: res.append(view[i]) return res ob = Solution() root = TreeNode(5) root.left = TreeNode(3) root.right = TreeNode(8) root.right.left = TreeNode(7) root.right.right = TreeNode(6) root.right.left.left = TreeNode(2) root.right.right.right = TreeNode(9) print(ob.solve(root))
Input
root = TreeNode(5) root.left = TreeNode(3) root.right = TreeNode(8) root.right.left = TreeNode(7) root.right.right = TreeNode(6) root.right.left.left = TreeNode(2) root.right.right.right = TreeNode(9)
Output
[3, 5, 8, 6, 9]
- Related Articles
- Program to print nodes in the Top View of Binary Tree using C++
- Program to find the left side view of a binary tree in C++
- Program to find the maximum width of a binary tree in Python
- Program to find sibling value of a binary tree node in Python
- Program to find number of only child in a binary tree in python
- Program to invert a binary tree in Python
- Program to find longest even value path of a binary tree in Python
- Program to find most frequent subtree sum of a binary tree in Python
- Python Program to Find the Sum of All Nodes in a Binary Tree
- Program to find second deepest node in a binary tree in python
- Program to find largest sum of any path of a binary tree in Python
- Program to find length of longest consecutive path of a binary tree in python
- Program to find length of longest alternating path of a binary tree in python
- Program to find k-length paths on a binary tree in Python
- Program to find leaf and non-leaf nodes of a binary tree in Python
