Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top
Given a pointer to the root of a binary tree, print the top view of the binary tree.
The tree as seen from the top the nodes, is called the top view of the tree.
For example :
table
Top View :
Complete the function topView and print the resulting values on a single line separated by space.
Input Format
You are given a function,
void topViewnode root
Constraints
Nodes the tree
Output Format
Print the values on a single line separated by space.
Sample Input
table
Sample Output
Explanation
table
From the top, only nodes are visible. We are given this code to work with:
import java.util.;
class Node
Node left;
Node right;
int data;
Node int data
this. data data;
left null;
right null;
class Solution
class Node
int data;
Node left;
Node right;
public static void topViewNode root
public static Node insertNode root, int data
if root null
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started