Understanding the Problem
A Sum Tree is a special type of binary tree where each non-leaf node's value is equal to the sum of the values of its left and right subtrees. Leaf nodes are considered valid by default. The goal is to check whether the given binary tree satisfies this property throughout all of its nodes.
We will solve this step by step using an example, and understand how to recursively validate the sum tree condition for each node, making it beginner-friendly with intuitive logic.
Step-by-Step Solution with Example
Step 1: Consider a sample binary tree
26
/ 10 3
/ 4 6 3
We need to check whether this entire tree is a Sum Tree. We will do this recursively, starting from the bottom-most nodes and moving upward.
Step 2: Check leaf nodes
In this tree, nodes 4
, 6
, and the right 3
are all leaf nodes. Since leaf nodes are always considered valid Sum Trees (they have no children to compare with), we mark them as valid.
Step 3: Check node with value 10
Now move up to the node 10
. Its left and right children are 4
and 6
respectively. Since both are leaves and valid, and 10 = 4 + 6
, node 10 satisfies the Sum Tree condition.
Step 4: Check node with value 3 (right child of root)
This node only has a right child: another 3
, which is a leaf. Since this node doesn’t have a left child, we treat its left sum as 0. So 3 = 0 + 3
, which is valid.
Step 5: Check root node 26
The left subtree rooted at 10 sums to 10
, and the right subtree rooted at 3 sums to 6
(3 + 3). So, we compute total child sum = 10 + 6 = 16. But wait — that’s incorrect. Actually, the right subtree has value 3 (node) + 3 (leaf) = 6, but the subtree rooted at 10 includes 4 and 6, so total left sum = 4 + 6 + 10 = 20, which is not allowed. That’s not how Sum Tree validation works.
Correction: When validating as a Sum Tree, we should not include the parent node’s value. Instead, we only use child subtree sums. So:
- Left subtree (10): total sum = 4 + 6 = 10
- Right subtree (3): total sum = 3 (right leaf only)
- So total = 10 + 3 = 13, but root is 26
This seems inconsistent. Let's re-define how we compute the subtree sums to include all children correctly. Actually, the definition is: a node’s value must be equal to the sum of values in its left and right subtrees (including all their nodes, not just their direct children). To handle this precisely, we need to return both whether the subtree is a Sum Tree and what its total sum is.
So for the given tree:
- 4 and 6 → leaves → sum = 4 and 6
- 10 = 4 + 6 → valid → sum = 10
- Leaf 3 → sum = 3
- Right 3 (with child 3) → sum = 6 → 3 = 3 (valid) → sum = 6
- Now check root 26 → left subtree sum = 10, right subtree sum = 6 → 10 + 6 = 16 ≠ 26 ❌
So the tree fails the Sum Tree condition at the root. The correct tree should be:
26
/ 10 16
/ / 4 6 7 9
Now it satisfies: 10 = 4 + 6, 16 = 7 + 9, 26 = 10 + 16 ✅
Edge Cases
Case 1: Empty Tree
An empty tree is trivially considered a Sum Tree. There are no nodes to violate the rule. So the result is true.
Case 2: Single Node Tree
A tree with a single node (a leaf) is always a valid Sum Tree because there are no children to compare. So, it's true.
Case 3: Violation at Any Node
Even if one node (that’s not a leaf) fails the condition node.val ≠ leftSum + rightSum
, the whole tree is invalid as a Sum Tree. The check must be applied recursively across the entire structure.
Case 4: Subtree Satisfies, But Root Violates
If all the child subtrees are valid but the root itself violates the rule, then the tree is still not a Sum Tree. Every node must obey the property — including the root.
Finally
To determine if a binary tree is a Sum Tree:
- Use post-order traversal (left-right-root).
- At each node, get the sum of left and right subtrees and verify the condition.
- Return both the validity and total sum from each subtree check.
This approach ensures we check all nodes while efficiently calculating and validating the required sum property. Edge cases like empty trees and single nodes are handled by default in the recursive logic.
Comments
Loading comments...