The top view of a binary tree is the set of nodes visible when the tree is viewed from the top. Given a binary tree, print the top view of it. The output nodes can be printed in any order. A node x is there in the output if x is the topmost node at its horizontal distance. The horizontal distance of the left child of a node x is equal to a horizontal distance of x minus 1, and that of a right child is the horizontal distance of x plus 1.
Examples:
Input: 1
/ \
2 3
/ \ / \
4 5 6 7
Output: Top view of the above binary tree is: 4 2 1 3 7
Input: 1
/ \
2 3
\
4
\
5
\
6
Output: Top view of the above binary tree is: 2 1 3 6
Approach: To solve the problem follow the below idea:
The idea is to do something similar to vertical Order Traversal. Like vertical Order Traversal, we need to put nodes of the same horizontal distance together. We do a level order traversal so that the topmost node at a horizontal node is visited before any other node of the same horizontal distance below it. Hashing is used to check if a node at a given horizontal distance is seen or not.
Follow the below steps to solve the problem:
- Create a function to print the top view of the binary tree
- If the root is equal to the null value then return from the function (Base case)
- Create a queue of type Node* and a map of type <int, int> and a variable hd to calculate the horizontal distance
- Set hd of the root equal to zero and push it into the queue
- Run a while loop till the queue is not empty
- set hd equal to root->hd
- Check if this hd already exists in the map, If not then set this node->val in map[hd]
- If root->left exists then set its hd = root->hd – 1 and push it into the queue
- If root->right exists then set its hd = root->hd + 1 and push it into the queue
- Pop the front element of the queue and set its value in the root
- Print the answer using the map
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
struct Node {
Node* left;
Node* right;
int hd;
int data;
};
Node* newNode( int key)
{
Node* node = new Node();
node->left = node->right = NULL;
node->data = key;
return node;
}
void topview(Node* root)
{
if (root == NULL)
return ;
queue<Node*> q;
map< int , int > m;
int hd = 0;
root->hd = hd;
q.push(root);
cout << "The top view of the tree is : \n" ;
while (q.size()) {
hd = root->hd;
if (m.count(hd) == 0)
m[hd] = root->data;
if (root->left) {
root->left->hd = hd - 1;
q.push(root->left);
}
if (root->right) {
root->right->hd = hd + 1;
q.push(root->right);
}
q.pop();
root = q.front();
}
for ( auto i = m.begin(); i != m.end(); i++) {
cout << i->second << " " ;
}
}
int main()
{
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->right = newNode(4);
root->left->right->right = newNode(5);
root->left->right->right->right = newNode(6);
topview(root);
return 0;
}
|
Java
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Queue;
import java.util.TreeMap;
class Node {
int data;
Node left, right;
public Node( int data)
{
this .data = data;
left = right = null ;
}
}
class BinaryTree {
Node root;
public BinaryTree() { root = null ; }
private void TopView(Node root)
{
class QueueObj {
Node node;
int hd;
QueueObj(Node node, int hd)
{
this .node = node;
this .hd = hd;
}
}
Queue<QueueObj> q = new LinkedList<QueueObj>();
Map<Integer, Node> topViewMap
= new TreeMap<Integer, Node>();
if (root == null ) {
return ;
}
else {
q.add( new QueueObj(root, 0 ));
}
System.out.println(
"The top view of the tree is : " );
while (!q.isEmpty()) {
QueueObj tmpNode = q.poll();
if (!topViewMap.containsKey(tmpNode.hd)) {
topViewMap.put(tmpNode.hd, tmpNode.node);
}
if (tmpNode.node.left != null ) {
q.add( new QueueObj(tmpNode.node.left,
tmpNode.hd - 1 ));
}
if (tmpNode.node.right != null ) {
q.add( new QueueObj(tmpNode.node.right,
tmpNode.hd + 1 ));
}
}
for (Map.Entry<Integer, Node> entry :
topViewMap.entrySet()) {
System.out.print(entry.getValue().data + " " );
}
}
public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();
tree.root = new Node( 1 );
tree.root.left = new Node( 2 );
tree.root.right = new Node( 3 );
tree.root.left.right = new Node( 4 );
tree.root.left.right.right = new Node( 5 );
tree.root.left.right.right.right = new Node( 6 );
tree.TopView(tree.root);
}
}
|
Python3
class newNode:
def __init__( self , key):
self .data = key
self .left = None
self .right = None
self .hd = 0
def topview(root):
if (root = = None ):
return
q = []
m = dict ()
hd = 0
root.hd = hd
q.append(root)
while ( len (q)):
root = q[ 0 ]
hd = root.hd
if hd not in m:
m[hd] = root.data
if (root.left):
root.left.hd = hd - 1
q.append(root.left)
if (root.right):
root.right.hd = hd + 1
q.append(root.right)
q.pop( 0 )
for i in sorted (m):
print (m[i], end = " " )
if __name__ = = '__main__' :
root = newNode( 1 )
root.left = newNode( 2 )
root.right = newNode( 3 )
root.left.right = newNode( 4 )
root.left.right.right = newNode( 5 )
root.left.right.right.right = newNode( 6 )
print ( "The top view of the tree is: " )
topview(root)
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class Node {
public int data;
public Node left, right;
public Node( int data)
{
this .data = data;
left = right = null ;
}
};
class QueueObj {
public Node node;
public int hd;
public QueueObj(Node node, int hd)
{
this .node = node;
this .hd = hd;
}
};
class BinaryTree {
Node root;
public BinaryTree() { root = null ; }
void TopView(Node root)
{
Queue q = new Queue();
SortedDictionary< int , Node> topViewMap
= new SortedDictionary< int , Node>();
if (root == null ) {
return ;
}
else {
q.Enqueue( new QueueObj(root, 0));
}
while (q.Count != 0) {
QueueObj tmpNode = (QueueObj)q.Dequeue();
if (!topViewMap.ContainsKey(tmpNode.hd)) {
topViewMap[tmpNode.hd] = tmpNode.node;
}
if (tmpNode.node.left != null ) {
q.Enqueue( new QueueObj(tmpNode.node.left,
tmpNode.hd - 1));
}
if (tmpNode.node.right != null ) {
q.Enqueue( new QueueObj(tmpNode.node.right,
tmpNode.hd + 1));
}
}
foreach ( var entry in topViewMap.Values)
{
Console.Write(entry.data);
Console.Write( " " );
}
}
public static void Main( string [] args)
{
BinaryTree tree = new BinaryTree();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.right = new Node(4);
tree.root.left.right.right = new Node(5);
tree.root.left.right.right.right = new Node(6);
Console.WriteLine( "The top view of the tree is : " );
tree.TopView(tree.root);
}
}
|
Javascript
<script>
class Node
{
constructor(data)
{
this .data=data;
this .left = this .right = null ;
this .hd = 0;
}
}
function topview(root)
{
if (root == null )
return ;
let q = [];
let m = new Map();
let hd = 0;
root.hd = hd;
q.push(root);
while (q.length!=0)
{
root = q[0];
hd = root.hd;
if (!m.has(hd))
m.set(hd,root.data);
if (root.left)
{
root.left.hd = hd - 1;
q.push(root.left);
}
if (root.right)
{
root.right.hd = hd + 1;
q.push(root.right);
}
q.shift()
}
let arr = Array.from(m);
arr.sort( function (a,b){ return a[0]-b[0];})
for (let [key, value] of arr.values())
{
document.write(value+ " " );
}
}
let root = new Node(1)
root.left = new Node(2)
root.right = new Node(3)
root.left.right = new Node(4)
root.left.right.right = new Node(5)
root.left.right.right.right = new Node(6)
document.write( "Following are nodes in top" ,
"view of Binary Tree<br>" )
topview(root)
</script>
|
Output
The top view of the tree is :
2 1 3 6
Time complexity: O(N * log(N)), where N is the number of nodes in the given tree.
Auxiliary Space: O(N), As we store nodes in the map and queue.
Above approach using HashMap for Java
Since we need the horizontal distance in sorted order TreeMap was used in the above solution; but instead, a minimum and maximum horizontal distance variable can be maintained for each iteration. After that traverse from minimum to the maximum while printing the first acquired node during the traversal of the tree that was stored in the map. Since, each horizontal distance from minimum to maximum is guaranteed to have at least one node in the map
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
struct Node{
int data;
Node* left;
Node* right;
Node( int data){
this ->data = data;
this ->left = NULL;
this ->right = NULL;
}
};
struct QueueObj{
Node* node;
int hd;
QueueObj(Node *node, int hd){
this ->node = node;
this ->hd = hd;
}
};
void topView(Node* root){
if (root == NULL) return ;
queue<QueueObj*> q;
map< int , int > mp;
int mn = 0;
int mx = 0;
q.push( new QueueObj(root, 0));
while (!q.empty()){
QueueObj* curr = q.front();
q.pop();
if (mp.find(curr->hd) == mp.end()){
mp[curr->hd] = curr->node->data;
}
if (curr->node->left != NULL){
mn = min(mn, curr->hd-1);
q.push( new QueueObj(curr->node->left, curr->hd-1));
}
if (curr->node->right != NULL){
mx = max(mx, curr->hd+1);
q.push( new QueueObj(curr->node->right, curr->hd+1));
}
}
for (; mn<=mx; mn++){
cout<<mp[mn]<< " " ;
}
cout<<endl;
}
int main(){
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->right = new Node(4);
root->left->right->right = new Node(5);
root->left->right->right->right = new Node(6);
cout<< "Following are nodes in top view of Binary Tree" <<endl;
topView(root);
return 0;
}
|
Java
import java.util.*;
class GFG {
static class Node {
Node left;
Node right;
int data;
Node( int data)
{
this .left = this .right = null ;
this .data = data;
}
}
static class QueueObj {
Node node;
int hd;
QueueObj(Node node, int hd)
{
this .node = node;
this .hd = hd;
}
}
static void topView(Node root)
{
if (root == null )
return ;
Queue<QueueObj> q = new LinkedList<>();
Map<Integer, Integer> map = new HashMap<>();
int min = 0 ;
int max = 0 ;
q.add( new QueueObj(root, 0 ));
while (!q.isEmpty()) {
QueueObj curr = q.poll();
if (!map.containsKey(curr.hd)) {
map.put(curr.hd, curr.node.data);
}
if (curr.node.left != null ) {
min = Math.min(min, curr.hd - 1 );
q.add( new QueueObj(curr.node.left,
curr.hd - 1 ));
}
if (curr.node.right != null ) {
max = Math.max(max, curr.hd + 1 );
q.add( new QueueObj(curr.node.right,
curr.hd + 1 ));
}
}
for (; min <= max; min++) {
System.out.print(map.get(min) + " " );
}
}
public static void main(String args[])
{
Node root = new Node( 1 );
root.left = new Node( 2 );
root.right = new Node( 3 );
root.left.right = new Node( 4 );
root.left.right.right = new Node( 5 );
root.left.right.right.right = new Node( 6 );
System.out.println( "Following are nodes in"
+ " top view of Binary Tree" );
topView(root);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
class QueueObj:
def __init__( self , node, hd):
self .node = node
self .hd = hd
def topView(root):
if root is None :
return
q = []
mp = {}
mn = 0
mx = 0
q.append(QueueObj(root, 0 ))
while len (q) ! = 0 :
curr = q.pop( 0 )
if curr.hd not in mp:
mp[curr.hd] = curr.node.data
if curr.node.left is not None :
mn = min (mn, curr.hd - 1 )
q.append(QueueObj(curr.node.left, curr.hd - 1 ))
if curr.node.right is not None :
mx = max (mx, curr.hd + 1 )
q.append(QueueObj(curr.node.right, curr.hd + 1 ))
for hd in range (mn, mx + 1 ):
print (mp[hd],end = " " )
root = Node( 1 )
root.left = Node( 2 )
root.right = Node( 3 )
root.left.right = Node( 4 )
root.left.right.right = Node( 5 )
root.left.right.right.right = Node( 6 )
print ( "Following are nodes in top view of Binary Tree" )
topView(root)
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public class Node {
public Node left;
public Node right;
public int data;
public Node( int data)
{
this .left = this .right = null ;
this .data = data;
}
}
public class QueueObj {
public Node node;
public int hd;
public QueueObj(Node node, int hd)
{
this .node = node;
this .hd = hd;
}
}
static void topView(Node root)
{
if (root == null )
return ;
Queue<QueueObj> q = new Queue<QueueObj>();
Dictionary< int , int > map
= new Dictionary< int , int >();
int min = 0;
int max = 0;
q.Enqueue( new QueueObj(root, 0));
while (q.Count != 0) {
QueueObj curr = q.Dequeue();
if (!map.ContainsKey(curr.hd)) {
map.Add(curr.hd, curr.node.data);
}
if (curr.node.left != null ) {
min = Math.Min(min, curr.hd - 1);
q.Enqueue( new QueueObj(curr.node.left,
curr.hd - 1));
}
if (curr.node.right != null ) {
max = Math.Max(max, curr.hd + 1);
q.Enqueue( new QueueObj(curr.node.right,
curr.hd + 1));
}
}
for (; min <= max; min++) {
Console.Write(map[min] + " " );
}
}
public static void Main( string [] args)
{
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.right = new Node(4);
root.left.right.right = new Node(5);
root.left.right.right.right = new Node(6);
Console.WriteLine( "Following are nodes in"
+ " top view of Binary Tree" );
topView(root);
}
}
|
Javascript
class Node{
constructor(data){
this .data = data;
this .left = null ;
this .right = null ;
}
}
class QueueObj{
constructor(node, hd){
this .node = node;
this .hd = hd;
}
}
function topView(root){
if (root == null ) return ;
let q = [];
let mp = new Map();
let mn = 0;
let mx = 0;
q.push( new QueueObj(root, 0));
while (q.length != 0){
let curr = q.shift();
if (mp[curr.hd] == null ){
mp[curr.hd] = curr.node.data;
}
if (curr.node.left != null ){
mn = Math.min(mn, curr.hd-1);
q.push( new QueueObj(curr.node.left, curr.hd-1));
}
if (curr.node.right != null ){
mx = Math.max(mx, curr.hd+1);
q.push( new QueueObj(curr.node.right, curr.hd+1));
}
}
while (mn <= mx){
console.log(mp[mn]);
mn++;
}
}
let root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.right = new Node(4);
root.left.right.right = new Node(5);
root.left.right.right.right = new Node(6);
console.log( "Following are nodes in top view of Binary Tree" );
topView(root);
|
Output
Following are nodes in top view of Binary Tree
2 1 3 6
Time Complexity: O(N), Since we only perform level-order traversal and print some part of the N nodes which at max will be 2N in case of skew tree
Auxiliary Space: O(N), Since we store the nodes in the map and queue.
Approach: To solve the problem follow the below idea:
Here we use the two variables, one for the vertical distance of the current node from the root and another for the depth of the current node from the root. We use the vertical distance for indexing. If one node with the same vertical distance comes again, we check if the depth of the new node is lower or higher with respect to the current node with the same vertical distance in the map. If the depth of the new node is lower, then we replace it
Follow the below steps to solve the problem:
- Create a map of the type <int, pair<int, int>> and two variables d and l to store horizontal and vertical distance from the root respectively
- Call the function to print the top view
- If the root is equal to the null value then return from the function (Base case)
- Check if this value of d is not present in the map, then set map[d] equal to {root->data, l}
- Check if this value of d is already present and its vertical distance is greater than l, then set map[d] equal to {root->data, l}
- Call this function recursively for (root->left, d-1, l+1, mp) and (root->right, d+1, l+1, mp)
- Print the top view of the binary tree using the map
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
Node* left;
Node* right;
int data;
};
Node* newNode( int key)
{
Node* node = new Node();
node->left = node->right = NULL;
node->data = key;
return node;
}
void fillMap(Node* root, int d, int l,
map< int , pair< int , int > >& m)
{
if (root == NULL)
return ;
if (m.count(d) == 0) {
m[d] = make_pair(root->data, l);
}
else if (m[d].second > l) {
m[d] = make_pair(root->data, l);
}
fillMap(root->left, d - 1, l + 1, m);
fillMap(root->right, d + 1, l + 1, m);
}
void topView( struct Node* root)
{
map< int , pair< int , int > > m;
fillMap(root, 0, 0, m);
for ( auto it = m.begin(); it != m.end(); it++) {
cout << it->second.first << " " ;
}
}
int main()
{
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->right = newNode(4);
root->left->right->right = newNode(5);
root->left->right->right->right = newNode(6);
cout << "Following are nodes in top view of Binary "
"Tree\n" ;
topView(root);
return 0;
}
|
Java
import java.util.*;
class GFG {
static class Node {
Node left;
Node right;
int data;
}
static class pair {
int first, second;
pair() {}
pair( int i, int j)
{
first = i;
second = j;
}
}
static TreeMap<Integer, pair> m = new TreeMap<>();
static Node newNode( int key)
{
Node node = new Node();
node.left = node.right = null ;
node.data = key;
return node;
}
static void fillMap(Node root, int d, int l)
{
if (root == null )
return ;
if (m.get(d) == null ) {
m.put(d, new pair(root.data, l));
}
else if (m.get(d).second > l) {
m.put(d, new pair(root.data, l));
}
fillMap(root.left, d - 1 , l + 1 );
fillMap(root.right, d + 1 , l + 1 );
}
static void topView(Node root)
{
fillMap(root, 0 , 0 );
for (Map.Entry<Integer, pair> entry :
m.entrySet()) {
System.out.print(entry.getValue().first + " " );
}
}
public static void main(String args[])
{
Node root = newNode( 1 );
root.left = newNode( 2 );
root.right = newNode( 3 );
root.left.right = newNode( 4 );
root.left.right.right = newNode( 5 );
root.left.right.right.right = newNode( 6 );
System.out.println( "Following are nodes in"
+ " top view of Binary Tree" );
topView(root);
}
}
|
Python3
class newNode:
def __init__( self , key):
self .data = key
self .left = None
self .right = None
def fillMap(root, d, l, m):
if (root = = None ):
return
if d not in m:
m[d] = [root.data, l]
elif (m[d][ 1 ] > l):
m[d] = [root.data, l]
fillMap(root.left, d - 1 , l + 1 , m)
fillMap(root.right, d + 1 , l + 1 , m)
def topView(root):
m = {}
fillMap(root, 0 , 0 , m)
for it in sorted (m.keys()):
print (m[it][ 0 ], end = " " )
root = newNode( 1 )
root.left = newNode( 2 )
root.right = newNode( 3 )
root.left.right = newNode( 4 )
root.left.right.right = newNode( 5 )
root.left.right.right.right = newNode( 6 )
print ( "Following are nodes in top view of Binary Tree" )
topView(root)
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG {
class Node {
public Node left;
public Node right;
public int data;
}
class pair {
public int first, second;
public pair( int i, int j)
{
first = i;
second = j;
}
}
static SortedDictionary< int , pair> m
= new SortedDictionary< int , pair>();
static Node newNode( int key)
{
Node node = new Node();
node.left = node.right = null ;
node.data = key;
return node;
}
static void fillMap(Node root, int d, int l)
{
if (root == null )
return ;
if (!m.ContainsKey(d)) {
m[d] = new pair(root.data, l);
}
else if (m[d].second > l) {
m[d] = new pair(root.data, l);
}
fillMap(root.left, d - 1, l + 1);
fillMap(root.right, d + 1, l + 1);
}
static void topView(Node root)
{
fillMap(root, 0, 0);
foreach (pair entry in m.Values)
{
Console.Write(entry.first + " " );
}
}
public static void Main( string [] args)
{
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.right = newNode(4);
root.left.right.right = newNode(5);
root.left.right.right.right = newNode(6);
Console.WriteLine( "Following are nodes in"
+ " top view of Binary Tree" );
topView(root);
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .data = 0;
this .left = this .right = null ;
}
}
class pair
{
constructor(i, j)
{
this .first = i;
this .second = j;
}
}
let m = new Map();
function newNode(key)
{
let node = new Node();
node.left = node.right = null ;
node.data = key;
return node;
}
function fillMap(root, d, l)
{
if (root == null )
return ;
if (m.get(d) == null ) {
m.set(d, new pair(root.data, l));
}
else if (m.get(d).second > l) {
m.set(d, new pair(root.data, l));
}
fillMap(root.left, d - 1, l + 1);
fillMap(root.right, d + 1, l + 1);
}
function topView(root)
{
fillMap(root, 0, 0);
let arr=Array.from(m.keys());
arr.sort( function (a,b){ return a-b;});
for (let key of arr.values()) {
document.write(m.get(key).first + " " );
}
}
let root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.right = newNode(4);
root.left.right.right = newNode(5);
root.left.right.right.right = newNode(6);
document.write( "Following are nodes in"
+ " top view of Binary Tree<br>" );
topView(root);
</script>
|
Output
Following are nodes in top view of Binary Tree
2 1 3 6
Time complexity: O(N * log(N)), where N is the number of nodes in the given binary tree with each insertion operation in Map requiring O(log2 N) complexity.
Auxiliary Space: O(N)
Approach: Follow the below steps to solve the problem:
- This approach is based on the level order traversal. We’ll keep a record of the current max so far left, and right horizontal distances from the root.
- And if we found less distance (or greater in magnitude) than max left so far distance then update it and also push data on this node to a stack (stack is used because in level order traversal the left nodes will appear in reverse order)
- If we found a greater distance then max right so far distance then update it and also push data on this node to a vector.
- In this approach, no map is used.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
Node *left, *right;
int data;
Node() { left = right = 0; }
Node( int data)
{
left = right = 0;
this ->data = data;
}
};
class Tree {
public :
Node* root;
Tree() { root = 0; }
void topView()
{
queue<pair<Node*, int > > q;
q.push(make_pair(root, 0));
int hd = 0, l = 0, r = 0;
stack< int > left;
vector< int > right;
Node* node;
while (q.size()) {
node = q.front().first;
hd = q.front().second;
if (hd < l) {
left.push(node->data);
l = hd;
}
else if (hd > r) {
right.push_back(node->data);
r = hd;
}
if (node->left) {
q.push(make_pair(node->left, hd - 1));
}
if (node->right) {
q.push(make_pair(node->right, hd + 1));
}
q.pop();
}
while (left.size()) {
cout << left.top() << " " ;
left.pop();
}
cout << root->data << " " ;
for ( auto x : right) {
cout << x << " " ;
}
}
};
int main()
{
Tree t;
t.root = new Node(1);
t.root->left = new Node(2);
t.root->right = new Node(3);
t.root->left->right = new Node(4);
t.root->left->right->right = new Node(5);
t.root->left->right->right->right = new Node(6);
t.topView();
cout << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
static class Node {
Node left;
Node right;
int data;
}
static class pair {
Node node;
int hd;
pair() {}
pair(Node node, int hd)
{
this .node = node;
this .hd = hd;
}
}
static Node newNode( int key)
{
Node node = new Node();
node.left = node.right = null ;
node.data = key;
return node;
}
static void topView(Node root)
{
Queue<pair> q = new LinkedList<>();
q.add( new pair(root, 0 ));
int hd = 0 , l = 0 , r = 0 ;
Stack<Integer> left = new Stack<>();
ArrayList<Integer> right = new ArrayList<>();
Node node = null ;
while (!q.isEmpty()) {
node = q.peek().node;
hd = q.peek().hd;
if (hd < l) {
left.push(node.data);
l = hd;
}
if (hd > r) {
right.add(node.data);
r = hd;
}
if (node.left != null ) {
q.add( new pair(node.left, hd - 1 ));
}
if (node.right != null ) {
q.add( new pair(node.right, hd + 1 ));
}
q.poll();
}
while (!left.isEmpty()) {
System.out.print(left.peek() + " " );
left.pop();
}
System.out.print(root.data + " " );
for ( int d : right) {
System.out.print(d + " " );
}
}
public static void main(String args[])
{
Node root = newNode( 1 );
root.left = newNode( 2 );
root.right = newNode( 3 );
root.left.right = newNode( 4 );
root.left.right.right = newNode( 5 );
root.left.right.right.right = newNode( 6 );
topView(root);
}
}
|
Python3
class Node:
def __init__( self , data):
self .left = None
self .right = None
self .data = data
class Tree:
def __init__( self ):
self .root = None
def topView( self ):
q = []
q.append(( self .root, 0 ))
hd = 0
l = 0
r = 0
left = []
right = []
while len (q) > 0 :
node, hd = q[ 0 ]
if hd < l:
left.append(node.data)
l = hd
elif hd > r:
right.append(node.data)
r = hd
if node.left ! = None :
q.append((node.left, hd - 1 ))
if node.right ! = None :
q.append((node.right, hd + 1 ))
q.pop( 0 )
while len (left) > 0 :
x = left.pop()
print (x, end = " " )
print ( self .root.data, end = " " )
for x in right:
print (x, end = " " )
if __name__ = = '__main__' :
t = Tree()
t.root = Node( 1 )
t.root.left = Node( 2 )
t.root.right = Node( 3 )
t.root.left.right = Node( 4 )
t.root.left.right.right = Node( 5 )
t.root.left.right.right.right = Node( 6 )
t.topView()
print ()
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG {
public class Node {
public Node left, right;
public int data;
}
public class pair {
public Node node;
public int hd;
public pair() {}
public pair(Node node, int hd)
{
this .node = node;
this .hd = hd;
}
}
static Node newNode( int key)
{
Node node = new Node();
node.left = node.right = null ;
node.data = key;
return node;
}
static void topView(Node root)
{
Queue<pair> q = new Queue<pair>();
q.Enqueue( new pair(root, 0));
int hd = 0, l = 0, r = 0;
Stack< int > left = new Stack< int >();
List< int > right = new List< int >();
Node node = null ;
while (q.Count != 0) {
node = q.Peek().node;
hd = q.Peek().hd;
if (hd < l) {
left.Push(node.data);
l = hd;
}
if (hd > r) {
right.Add(node.data);
r = hd;
}
if (node.left != null ) {
q.Enqueue( new pair(node.left, hd - 1));
}
if (node.right != null ) {
q.Enqueue( new pair(node.right, hd + 1));
}
q.Dequeue();
}
while (left.Count != 0) {
Console.Write(left.Peek() + " " );
left.Pop();
}
Console.Write(root.data + " " );
foreach ( int d in right) { Console.Write(d + " " ); }
}
public static void Main( string [] args)
{
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.right = newNode(4);
root.left.right.right = newNode(5);
root.left.right.right.right = newNode(6);
topView(root);
}
}
|
Javascript
class Node{
constructor(data){
this .data = data;
this .left = null ;
this .right = null ;
}
}
class pair{
constructor(val1, val2){
this .first = val1;
this .second = val2;
}
}
function topView(root){
let q = [];
q.push( new pair(root, 0));
let hd = 0, l = 0, r = 0;
let left = [];
let right = [];
let node;
while (q.length > 0){
let temp = q.shift();
node = temp.first;
hd = temp.second;
if (hd < l){
left.push(node.data);
l = hd;
}
else if (hd > r){
right.push(node.data);
r = hd;
}
if (node.left != null ){
q.push( new pair(node.left, hd - 1));
}
if (node.right != null ){
q.push( new pair(node.right, hd + 1));
}
}
while (left.length > 0){
document.write(left.pop() + " " );
}
document.write(root.data + " " );
for (let i = 0; i<right.length; i++) {
document.write(right[i] + " " );
}
}
let root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.right = new Node(4);
root.left.right.right = new Node(5);
root.left.right.right.right = new Node(6);
topView(root);
|
Time Complexity: O(N), where N is the number of nodes in the given binary tree.
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
19 Jan, 2023
Like Article
Save Article