• + 5 comments

    This question is just about printing the outermost left or right nodes .. java code (its a version of what parmarabhishek_1 told https://www.hackerrank.com/challenges/tree-top-view/forum/comments/331313 )

    java code

    void travLeft(Node root){
        if(root.left!= null)
              { travLeft(root.left);}
           System.out.printf("%d ",root.data);
                   }
        void travRight(Node root){
         System.out.printf("%d ",root.data);
         if(root.right!= null){
             travRight(root.right);}
        }
    
       void topView(Node root) {
           if(root.left != null){
                        travLeft(root.left);
                                }
              System.out.printf("%d ",root.data);
           if(root.right != null){
                        travRight(root.right);
                                }      
        }