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.
- Prepare
- Data Structures
- Trees
- Tree : Top View
- Discussions
Tree : Top View
Tree : Top View
Sort by
recency
|
970 Discussions
|
Please Login in order to post a comment
Here is Tree: Top View solution in Python, Java, C++ and c programming - https://programmingoneonone.com/hackerrank-tree-top-view-problem-solution.html
Editorial solution complicates the problem into a whole new dimension. This BFS of mine is more intuitive, efficient and faster. Hope this helps :)
Output Buffer Logic: - According to the constraint, there are exactly 500 nodes in the tree in the worst case.
So, I created a raw array where negative "horizontal distance" vlaues will be placed from index 0 to 499 and positive values will be placed from index 499 to 999.
Then I did simple index mapping arithmetic and get the job done. That's all.
In Java 15 , I can neither create Node class on my own nor there is anything defined . I guess platform needs to correct it
Terrible question. Doesn't provide an example for the left side and the logical correct answer is somehow wrong.
Python code:
from collections import deque
def topView(root):