← All questions
MediumCoding

Sum Root to Leaf Numbers

Asked atBitGoMeta
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. 01Carry the numeric prefix from root to current node.
  2. 02Extend it as current times ten plus node value.
  3. 03At a leaf, return the complete number.
  4. 04Sum left and right recursive results, handling null as zero.

Reference answer

Then expect these follow-ups

  • How would you collect the actual path strings?

    Tests: path state

  • What changes for base b digits?

    Tests: generalization

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