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.
Hackers beware! It appears that there was a major change in this problem (in the underlying test cases and checking, but sadly the problem description is highly ambiguous!) and it is no longer sufficient to print the left and right spines. This also invalidates most of the most upvoted comments and answers in this thread from 2-3 years ago. In my opinion, this problem should be labelled Medium or Hard, not Easy!
To pass all test cases, you must keep track of the locations of every note in the entire tree, assuming all edges are of the same length and same angle. Hence, nodes may show up in the top view even if they are not in a spine!
Here's a C++ solution that passes all test cases as of July 2018:
Dude you are Genius!!! I struggle to hybrid different Data Structures to come up with such clever solutions, hopefully i could reach that level someday.
how to do for the bottom view.
bcz i'm little confused about this code.
if pair formed with same value,which one wol be counted?
like pair(0,root) and pair(0,root->right->left)
which one would print????
Man this solution was genius, took me some time to understand and i tried to solve the problem with this solution and it got the top rank on the leaderboard so if you see me on the leaderboard it is actually this guy!
Great code! as friends know, insert method when the key is already exists, does not insert and when we start with root, we can be sure that topmost nodes in each horizontal distance are inserted.
Tree : Top View
You are viewing a single comment's thread. Return to all comments →
Hackers beware! It appears that there was a major change in this problem (in the underlying test cases and checking, but sadly the problem description is highly ambiguous!) and it is no longer sufficient to print the left and right spines. This also invalidates most of the most upvoted comments and answers in this thread from 2-3 years ago. In my opinion, this problem should be labelled Medium or Hard, not Easy!
To pass all test cases, you must keep track of the locations of every note in the entire tree, assuming all edges are of the same length and same angle. Hence, nodes may show up in the top view even if they are not in a spine!
Here's a C++ solution that passes all test cases as of July 2018:
Looks like you made it through, from the previous comments heard that even editorial solutions isn't passing the test cases #strange.
Under 10 lines of code C++ solution
Actually there's no need to keep track of depth or to use another function, here's an example solution:
This is the most brilliant solution of this problem.
Dude you are Genius!!! I struggle to hybrid different Data Structures to come up with such clever solutions, hopefully i could reach that level someday.
this comment deserves a medal
how to do for the bottom view. bcz i'm little confused about this code. if pair formed with same value,which one wol be counted? like pair(0,root) and pair(0,root->right->left) which one would print????
Thanks! I got to learn a lot just by reading this code!
Legend took me time to get what you did
Just brilliant! When will I be able to think like this
Man this solution was genius, took me some time to understand and i tried to solve the problem with this solution and it got the top rank on the leaderboard so if you see me on the leaderboard it is actually this guy!
Great code! as friends know,
insert
method when the key is already exists, does not insert and when we start with root, we can be sure that topmost nodes in each horizontal distance are inserted.I really can't understand what the code actually does. Can anyone help me out?