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
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