Top View of a Binary Tree in Java17 Mar 2025 | 5 min read In this section, we will learn about the top view of a binary tree in Java and the different approaches to achieve it. In the top view of a binary tree, we print only those nodes of the binary tree that are visible when the binary tree is viewed from the top. For example, consider the following binary tree. ![]() The top view of the above binary tree is: For the following binary tree: ![]() The top view of the above binary tree is: Note: In the top view of a binary tree, the order in which the nodes are displayed in the output is not relevant. All one should care about is that all of the nodes that are visible from the top side of the binary tree should be included in the output.Approach 1: Using QueueIn this approach, we need to group the nodes whose horizontal distance is the same by taking the root node as the reference. We perform a level order traversal of the tree using a queue so that the topmost nodes are visited before the nodes that are lying below it. Hashing technique is used to identify whether a given horizontal distance is seen or not. ImplementationLet's see the implementation of the top view of a binary tree using the horizontal distance approach. FileName: TopViewExample.java Output: The following are the nodes present in the top view of the Binary Tree 7 4 3 2 1 Approach 2: Using Two VariablesIn the previous approach, we have discussed the top view of the tree using a queue. In this approach, we will be using two variables instead of a queue. One variable is used to identify the depth of a node taking the root node as a reference, and another for finding the vertical distance from the root node. For indexing, we use vertical distancing. If any other node with the same vertical distance is visited again, then we have to check whether the depth of the new node is higher or lower as compared to the current node. We do the replacement when the depth is lower of the new node. ImplementationLet's see the implementation of the top view of a binary tree using the two variables. FileName: TopViewExample1.java Output: The following are the nodes present in the top view of the Binary Tree 7 4 3 2 1 Next TopicTribonacci Series in Java |
Java 8 Vs. Java 11 Java has been used by programmers for generations, however, it has developed a lot through different versions of it. From the launch of Java 1.0 in 1996, it now forms the basis for platform-independent programming. J2SE 1.2 (otherwise called Java 2) introduced...
6 min read
It is a problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, Flipkart, etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are going to...
12 min read
In Java programming, the concept of null is both fundamental and ubiquitous. It represents the absence of a value for reference types and serves as a critical tool for developers to handle cases where objects or arrays are not initialized. Understanding null is essential for...
3 min read
While a String is in use, it is still feasible to reverse it. Since String in Java is immutable, it is neither possible to reverse the same String; nevertheless, by using changing StringBuilder or StringBuffer, you can limit the amount of intermediary String objects. The method...
3 min read
In the ever-evolving landscape of software development, programming languages continually adapt to meet the demands of modern application development. Java, a language known for its robustness and cross-platform capabilities, took a significant leap forward with the release of Java 9. One of the notable improvements that Java...
4 min read
An event is one of the most important concepts in Java. The change in the state of an object or behavior by performing actions is referred to as an Event in Java. Actions include button click, keypress, page scrolling, or cursor movement. Java provides a package java.awt.event...
7 min read
In Java, there are three types of statements that are declaration, expression, and control statements. Beside this, there is another statement is known as empty statement. In this section, we will discuss about the empty statement in Java with examples. Empty Statement As the name suggests, an empty...
4 min read
Matrix manipulation is a fundamental aspect of computer science and programming. In Java, developers often encounter scenarios where they need to perform various operations on matrices. One interesting matrix operation is flipping, and introducing randomness to this operation can add a unique dimension to your code....
4 min read
Traversing a matrix spirally entails moving through the elements in a circular pattern, starting from the top-left corner and moving to the right along the top row. Following every row or column traversal, the boundaries are adjusted, and the direction is switched, persisting until all elements...
10 min read
Arrays are also among the most fundamental, easiest, and simplest data structures in Java and many other languages. They help a developer store a number of values of the same kind in a single block of memory that is contiguous. Therefore, this makes access and...
6 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India