PPractick

DSA interview question

Children Sum Property

Medium · Data structures · Topics: tree, dfs

The problem

An inventory system models warehouses as a binary tree. Every warehouse's stock count must equal the sum of its two sub-warehouses' counts, with a missing child counting as 0, and leaves hold whatever they hold. An auditor must verify that every internal node satisfies this "children sum property". A traversal checks each node against its children. The whole tree passes only if every internal node does.

Example: [10, 8, 2, 3, 5, 1, 1] → true (10 = 8 + 2, 8 = 3 + 5, 2 = 1 + 1).

Function signature

bool isSumProperty(TreeNode* root);

Test cases

Input (level order)OutputExplanation
[10, 8, 2, 3, 5, 1, 1]true
[35, 20, 15, 15, 5, 10, 5]true
[1, 4, 3, 5]false1 ≠ 4 + 3
[3, 1, 2, 2]false1 has a single child 2
[5]trueA leaf has no constraint
[]true

(Confirm: the rule applies to every node with at least one child; a missing child counts as 0; leaves are always fine.)

How interviewers phrase it

Clarifying questions to ask

  1. How do leaves behave? (They satisfy it automatically.)
  2. Does a missing child count as 0? (Yes.)
  3. Return a bool, or 1/0? (Either.)

Follow-ups to expect

  1. "Recursive structure?"
  2. "Iterative?"
  3. "How would you convert any tree to satisfy it using only increments?"
  4. "Could values overflow?"
  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