Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

java solution please Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen

image text in transcribedimage text in transcribed

java solution please

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 : 1 1 2 1 5 11 3 1 4 Top View : 1>2>5>6 Complete the function topView and print the resulting values on a single line separated by space. Input Format You are given a function, void topView (node * root) \{ \} Constraints 1 Nodes in the tree 500 Output Format Print the values on a single line separated by space. Sample Input 1 1 2 1 5 11 3 6 1 4 Sample Output 1256 Explanation 1 1 2 1 5 11 36 1 4 From the top, only nodes 1,2,5,6 are visible. import java.util. *; import java.io.*; class Node \{ Node left; Node right; int data; Node(int data) \{ this. data = data; left = null; right = null; \} \} class solution \{ public static Node insert(Node root, int data) \{ if ( root == null ){ return new Node(data); \} else \{ Node cur; if (data = cur; \} else \{ cur = insert (root.right, data); root.right = cur; \} return root; \} \} public static void main(String[] args) \{ Scanner scan = new Scanner (System.in); int t= scan.nextInt () ; Node root = null; while (t>0){ int data = scan. nextInt () ; root = insert (root, data); \} scan.close (); topView(root); \} \}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

1 2 3 Data Base Techniques

Authors: Dick Andersen

1st Edition

0880223464, 978-0880223461

More Books

Students also viewed these Databases questions