Count Nodes Equal to Sum of Descendants - Problem

Imagine you're a tree auditor tasked with finding special nodes in a binary tree! Your job is to count how many nodes have a value that exactly equals the sum of all their descendants (children, grandchildren, etc.).

Given the root of a binary tree, return the number of nodes where the node's value equals the sum of all values in its subtree (excluding the node itself). If a node has no descendants (it's a leaf), consider its descendant sum as 0.

Example: In a tree where node 10 has descendants with values [3, 2, 5], we check if 10 == 3 + 2 + 5. If yes, this node counts toward our answer!

This problem tests your understanding of tree traversal and subtree sum calculation - essential skills for many tree-based algorithms used in system design and data processing.

Input & Output

example_1.py โ€” Basic Tree
$ Input: root = [10,3,4,2,1]
โ€บ Output: 2
๐Ÿ’ก Note: Node 10: descendants sum = 3+4+2+1 = 10 โœ“ (matches node value). Node 3: descendants sum = 2+1 = 3 โœ“ (matches node value). Nodes 4, 2, 1 are leaves with descendant sum 0, don't match their values.
example_2.py โ€” Single Node
$ Input: root = [2]
โ€บ Output: 1
๐Ÿ’ก Note: Single node with no descendants. Descendant sum = 0, but node value = 2. However, by problem definition, a node with no descendants has descendant sum 0, and we check if 2 == 0, which is false. Wait, let me reconsider - if it's asking for nodes equal to sum of descendants, and descendant sum is 0 for leaves, then only nodes with value 0 would count among leaves.
example_3.py โ€” All Zeros
$ Input: root = [0,0,0]
โ€บ Output: 3
๐Ÿ’ก Note: Root node 0: descendant sum = 0+0 = 0 โœ“. Left child 0: descendant sum = 0 (no children) โœ“. Right child 0: descendant sum = 0 (no children) โœ“. All three nodes qualify.

Constraints

  • The number of nodes in the tree is in the range [1, 105]
  • -105 โ‰ค Node.val โ‰ค 105
  • Note: Descendant sum is 0 for leaf nodes

Visualization

Tap to expand
Corporate Hierarchy AuditCEO$100kMGR$60kMGR$40kEMP$30kEMP$30kEMP$40kโœ“ Left MGR: $60k = $30k + $30k (team sum)โœ— CEO: $100k โ‰  $60k + $40k + $40k (team sum)โœ— Right MGR: $40k โ‰  $40k (team sum = 0)
Understanding the Visualization
1
Start from Bottom
Begin with leaf nodes (individual contributors) - their team sum is 0
2
Calculate Team Sums
For each manager, sum up their direct reports' total compensation
3
Check Equality
Compare manager's salary with their team's total - count matches
4
Propagate Upward
Pass team totals up the hierarchy for higher-level calculations
Key Takeaway
๐ŸŽฏ Key Insight: Post-order traversal naturally provides subtree sums needed for parent calculations, eliminating redundant work and achieving optimal O(n) time complexity.
Asked in
Amazon 45 Google 35 Meta 28 Microsoft 22
42.0K Views
Medium-High Frequency
~18 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen