Bottom View of a Binary Tree

Asked atMicrosoft
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below
0
Stuck? Show a way to structure it+
  1. 01Assign root horizontal distance zero.
  2. 02Traverse with a queue carrying node and distance.
  3. 03Move left to distance minus one and right to distance plus one.
  4. 04Overwrite the value stored for each distance as deeper or later nodes arrive.
  5. 05Output distances in sorted order.

Reference answer

Then expect these follow-ups

  • How does top view differ from bottom view?

    Tests: follow-up reasoning

  • Why does BFS make depth handling simpler?

    Tests: correctness reasoning

Hiring now

Get the Microsoft interview, not just the prep

landed applies to roles like these for you, on each company’s own site, with answers written for the role.

Get 5x interviews10 free auto-applies. No card.
Or practice this question first

Next question