PPractick

DSA interview question

Bottom View of a Binary Tree

Medium · Data structures · Topics: tree, bfs, hash map

The problem

Now look at the tree from below. For each horizontal distance (root 0, left −1, right +1), the visible node is the lowest one. If two nodes share the lowest level at the same distance, the one appearing later in level order wins. A BFS that keeps overwriting the entry for each distance ends with exactly that node, because BFS visits deeper levels later and, within a level, from left to right.

Example: [20, 8, 22, 5, 3, null, 25, null, null, 10, 14] → [5, 10, 3, 14, 25].

Function signature

vector<int> bottomView(TreeNode* root);

Test cases

Input (level order)OutputExplanation
[20, 8, 22, 5, 3, null, 25, null, null, 10, 14][5, 10, 3, 14, 25]10 and 14 hide 8 and 22
[20, 8, 22, 5, 3, 4, 25, null, null, 10, 14][5, 10, 4, 14, 25]3 and 4 tie at distance 0; the later one (4) wins
[1, 2, 3][2, 1, 3]
[1][1]
[][]

(Confirm: the lowest node per horizontal distance; ties on the same level go to the one later in level order.)

How interviewers phrase it

Clarifying questions to ask

  1. How are ties at the same depth and distance broken? (The later one in level order.)
  2. How is horizontal distance defined?
  3. Left-to-right output? (Yes, by increasing distance.)

Follow-ups to expect

  1. "How does this differ from the top view?"
  2. "Why is the last node seen the correct one?"
  3. "Would DFS work?"
  4. "Left or right views?"
  5. "Complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in