Return Maximum number of turns in a tree from root to lead node

Asked atNutanix
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. 01Clarify whether leaf node and left-right turns are intended
  2. 02Traverse from root carrying previous direction and count
  3. 03Increment only when current edge changes direction
  4. 04Update a global or returned maximum at leaves
  5. 05Test single paths and alternating directions

Reference answer

Then expect these follow-ups

  • How would you return the leaf path that achieves the maximum?

    Tests: path reconstruction

  • How would you write this iteratively with an explicit stack?

    Tests: tree traversal

Free to read · better with Enzo

Practice this out loud with Enzo

Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.

Next question