← All questions
MediumCoding

Cousins in Binary Tree II

Asked atAmazon
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. 01Set root value to zero.
  2. 02Process one level at a time.
  3. 03Compute total child value sum for the next level.
  4. 04For each parent, assign children total minus its sibling sum.
  5. 05Queue children after calculating from original values.

Reference answer

Then expect these follow-ups

  • Can DFS solve this with depth aggregates?

    Tests: follow-up reasoning

  • Why is the sibling sum subtracted for every child of one parent?

    Tests: correctness reasoning

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