We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Loading...
  • Practice
  • Compete
  • Jobs
  • Leaderboard
  • Hiring developers?
  1. Practice
  2. Data Structures
  3. Trees
  4. Tree : Top View
  5. Discussions

Tree : Top View

  • Problem
  • Submissions
  • Leaderboard
  • Discussions
  • Editorial

    You are viewing a single comment's thread. Return to all comments →

  • BansheeGhoul 2 years ago+ 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);
                                }      
        }
    
    -4|
    ParentPermalink
    • h415074476 2 years ago+ 1 comment

      you are right.

      -1|
      ParentPermalink
      • BarryB 2 years ago+ 0 comments

        Actually the problem being tested with the test cases is easy. However if you try to solve the problem as defined in GFC, I'd agree that it's not that easy. I actually spent some time trying to solve what GFC describes, but I couldn't get all the test cases to work. When I debugged some of the test cases, I realized that the test cases were all for the simplified case. I then modified my code to support that, and all test cases passed.

        After I got all the test cases to work, I actually looked at the editorial, and found the following.

        Note: This solution is overly simplified and you will need something more complicated for certain types of imbalanced trees.

        Basically the actual problem statement was not well defined. Either the test cases should support all types of trees, and the problem could potentially be made a medium problem. Or the note should have been put in the problem statement, instead of the editorial.

        0|
        ParentPermalink
    • mvbelgaonkar 1 year ago+ 0 comments
      [deleted]
      0|
      ParentPermalink
    • adityaverma82191 1 year ago+ 0 comments

      this will only pass the 1st test case. i thougth the same logic and i implemented it in c++.

      1|
      ParentPermalink
    • kyxap 1 year ago+ 0 comments

      Will not pass the 2nd TCs :)

      1|
      ParentPermalink
    • ioluwayo 4 months ago+ 0 comments

      This only passes the sample and first test case

      0|
      ParentPermalink
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature