
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to print nodes in the Top View of Binary Tree using C++
In this tutorial, we will be discussing a program to print all the nodes that appear in the top view of a given binary tree.
For a particular binary tree, a node appears in its top view if it is the very first node at its horizontal distance. Horizontal distance for the left node of a node x is x-1 and for the right node of node x is x+1.
To solve this, we will do the level order traversal so that we get the topmost node for a particular level before the other nodes present at that level. Further, we will use hashing to check whether the selected node is visible in the top view or not.
Example
#include#include #include
Output
Top View for the given tree: 23 11 35 68
Advertisements