0
0
DSA Typescriptprogramming~5 mins

Top View of Binary Tree in DSA Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the Top View of a binary tree?
The top view of a binary tree is the set of nodes visible when the tree is seen from above. It includes the nodes that are the first at each horizontal distance from the root.
Click to reveal answer
beginner
How do we determine the horizontal distance (HD) of nodes in a binary tree?
The root node has HD = 0. For any node, its left child has HD = parent's HD - 1, and its right child has HD = parent's HD + 1.
Click to reveal answer
intermediate
Why do we use a queue and a map/dictionary in the top view algorithm?
A queue helps in level order traversal (breadth-first), and a map stores the first node encountered at each horizontal distance to ensure only the topmost node is recorded.
Click to reveal answer
intermediate
What is the time complexity of finding the top view of a binary tree?
The time complexity is O(n), where n is the number of nodes, because each node is visited once during the level order traversal.
Click to reveal answer
intermediate
In the top view, what happens if two nodes share the same horizontal distance and level?
Only the first node encountered at that horizontal distance during level order traversal is included in the top view. Nodes at the same HD but lower levels are ignored.
Click to reveal answer
What data structure is commonly used to perform the traversal for top view of a binary tree?
AStack
BQueue
CPriority Queue
DLinked List
What is the horizontal distance of the root node in a binary tree?
A0
B1
C-1
DDepends on tree height
If two nodes have the same horizontal distance, which one appears in the top view?
AThe leftmost node
BThe node at the lower level
CThe node at the higher level (closer to root)
DThe rightmost node
Which data structure is used to store nodes by their horizontal distance in the top view algorithm?
AStack
BArray
CSet
DMap or Dictionary
What is the overall time complexity of the top view algorithm for a binary tree with n nodes?
AO(n)
BO(n log n)
CO(n^2)
DO(log n)
Explain how to find the top view of a binary tree step-by-step.
Think about how to see the tree from above and which nodes block others.
You got /5 concepts.
    Describe the role of horizontal distance in the top view of a binary tree.
    Imagine standing above the tree and looking down.
    You got /5 concepts.