Check for Children Sum Property - II
Problem Statement:β
Given a Binary Tree, convert the value of its nodes to follow the Children Sum Property. The Children Sum Property in a binary tree states that for every node, the sum of its children's values (if they exist) should be equal to the node's value. If a child is missing, it is considered as having a value of 0.
Note:
The node values can be increased by any positive integer any number of times, but decrementing any node value is not allowed.A value for a NULL node can be assumed as 0.We cannot change the structure of the given binary tree.
-
The node values can be increased by any positive integer any number of times, but decrementing any node value is not allowed.
-
A value for a NULL node can be assumed as 0.
-
We cannot change the structure of the given binary tree.
-
Example:
β Solution: DFS (Preorder + Postorder Adjustment)β
// Function to change the values of the nodes
// based on the sum of its children's values.
void changeTree(TreeNode* root) {
// Base case: If the current node is NULL, do nothing.
if (root == NULL) {
return;
}
// Step 1: Preorder Logic β enforce the Children Sum Property top-down
int child = 0;
if (root->left) {
child += root->left->val;
}
if (root->right) {
child += root->right->val;
}
if (child >= root->val) {
root->val = child;
} else {
// If child sum is smaller, push the root's value downward
if (root->left) {
root->left->val = root->val;
} else if (root->right) {
root->right->val = root->val;
}
}
// Step 2: Recur down the left and right subtrees
changeTree(root->left);
changeTree(root->right);
// Step 3: Postorder Logic β aggregate child values bottom-up
int tot = 0;
if (root->left) tot += root->left->val;
if (root->right) tot += root->right->val;
// If not a leaf, set root to total child sum
if (root->left || root->right) {
root->val = tot;
}
}
π How It Worksβ
This function modifies a binary tree so that it satisfies the Children Sum Property, i.e.,
every node's value becomes the sum of its left and right child values.
The logic involves two passes combined:
- Top-down (Preorder phase): Push values down to children when necessary.
- Bottom-up (Postorder phase): After fixing children, update the current node to reflect their final values.
π§© Key Concept / Formulaβ
-
If
childSum >= root->valβ setroot->val = childSum -
Else β propagate
root->valdownward to child nodes. -
After recursion, update:
root->val = left->val + right->val(if not a leaf)
β±οΈ Time & Space Complexityβ
| Metric | Complexity |
|---|---|
| Time | O(N) β each node visited once |
| Space | O(H) β recursion stack (H = tree height) |
β οΈ Edge Casesβ
- Tree is empty β no change
- Single node (leaf) β no change needed
- Only one child β handles correctly
- Already satisfying sum property β still valid
π‘ Other Approachesβ
| Approach | Time | Notes |
|---|---|---|
| DFS β | O(N) | Efficient, in-place |
| Level Order (BFS) | O(N) | Harder to propagate updates correctly |
π Related Problemsβ
- GFG: Children Sum Property
- Leetcode 124: Binary Tree Maximum Path Sum
- Leetcode 543: Diameter of Binary Tree
- Leetcode 437: Path Sum III
π οΈ Real-Life Analogyβ
Think of a manager (root) who must always earn the sum of the salaries of their team (left and right). If the teamβs salaries are too low, the manager splits their own salary to balance the structure.