Question: 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

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

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!