You are viewing a single comment's thread. Return to all comments →
When I visited this question there was no definition for top view given so I had to search for a definition. I came to the geeks for geeks link that others have referenced and made my algorithm according to geeks for geeks' definition which is the same as @Faland's and it was still accepted no problem. There was also only two test cases when I submitted. I honestly think you can submit either way and be fine unless my submission is not implementing geek for geek's definition of top view correctly.
If we follow algo given by geeksforgeeks then it doesnt give right answer. Geeks for geeks alfo print according @Faland
It Does, I have submitted the logic of vertical order traversal.
This is the actual code for top view of a tree :
typedef pair<int,int> v_h;
map <int,v_h> m; // map
map <int, v_h> :: iterator itr; //itterator
void store_view(node * root,int i,int h)
if(itr->second.first > h)
for(itr = m.begin(); itr != m.end(); ++itr)
void topView(node * root)
but the problem demands the top view according to the root node only :
void for_left(node * root)
void for_right(node * root)
void topView(node * root)
You are spot onn in identifying two different implementations .. looks like this question demands just the outer values of the tree. Thanks once again.
Isn't it the second solution that is expected - the first solution passes in July '18! Seems like they changed the meaning; and if so, I don't think this is a "easy" problem - a "medium" one.
it seems the second part of parmarabhishek_1's post was what the problem originally expected, and that was incorrect. They changed it to correctly require the solution in the first part, as you pointed out. They should have also used a better example to illustrate this subtle point.
it's not working for all the test cases
I think you are printing root->data 3 times in the topView() function.
Yep, the definition of "Top-View" was never presented in this problem. Besides the grammatical errors in the sentence "explaining" top-view, it almost literally says: when you view from the top, you'll see the top view.
The definition I found, that made sense was from the geeks for geeks link. So I solved it that way. I failed everything past the first test. So I looked at the input/output for the second test case.
For anyone interested, it's clear that the input is given as a binary search tree. So I constructed the graph from the input, ran my program, got the correct result but with extras (to the right for the second test case).
Then figured I was getting extras because a subtree was "poking" out and they didn't want that. So deleted all of my (more complicated) code for what amounted to a trivial problem.
can anyone explain how to represent the tree for this input, 47 2 40 20 38 30 14 28 10 16 19 44 39 27 7 9 31 12 43 21 5 41 34 49 13 33 3 4 25 22 29 15 32 35 6 24 23 26 1 11 42 36 37 17 18 8 45 48 50 46 ? what is root of this tree ?
Expected output: 1 2 47 49 50
My output: 47 2 1 49 50
@niranjan_wad how did you manage to find the output? Since, it does not tell what is your output. do you know what is root of above question ?
If you hover of the submission results that failed, there will be a popup that appears, where you can download both the input and expected output for that test case.
You appear to have the correct output, but in the wrong order. The solution wants it "left-to-right" (which just so happens to be least-to-greatest since these are apparently
binary search trees).
Try using TreeMap so the keys will be in sorted order
bro..first print traverse, then print that node. then u get correct order
first traverse,the print that node. u get correct order
The root is 47 (the first node given), the rest are input as a binary search tree, which means 2 is the left child of 47. 40 is the right child of 2 (because it's less than 47 but greater than 2), 20 is the left child of 40 (less than 47, greater than 2, and less than 40)...etc.