LeetCode Problem Workspace
Root Equals Sum of Children
Check if a binary tree root value equals the sum of its immediate left and right child nodes efficiently.
2
Topics
7
Code langs
3
Related
Practice Focus
Easy · Binary-tree traversal and state tracking
Answer-first summary
Check if a binary tree root value equals the sum of its immediate left and right child nodes efficiently.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Binary-tree traversal and state tracking
To solve Root Equals Sum of Children, simply compare the root node's value with the sum of its left and right children. This problem only involves three nodes, so direct value comparison avoids unnecessary recursion or traversal overhead. Ensure to handle negative values correctly since nodes can have negative integers.
Problem Statement
You are given a binary tree consisting of exactly three nodes: a root, a left child, and a right child. Your task is to determine whether the root's value equals the sum of its two children.
Return true if the root value is equal to the sum of the left and right child values, otherwise return false. Consider negative values and zero as valid node values.
Examples
Example 1
Input: root = [10,4,6]
Output: true
The values of the root, its left child, and its right child are 10, 4, and 6, respectively. 10 is equal to 4 + 6, so we return true.
Example 2
Input: root = [5,3,1]
Output: false
The values of the root, its left child, and its right child are 5, 3, and 1, respectively. 5 is not equal to 3 + 1, so we return false.
Constraints
- The tree consists only of the root, its left child, and its right child.
- -100 <= Node.val <= 100
Solution Approach
Direct Comparison
Access the root, left, and right child values directly and return root.val == left.val + right.val. This avoids traversal since the tree has exactly three nodes.
Recursive Safety Check
Although unnecessary for this small tree, a simple recursive check can be applied for consistency: return true if root is null, otherwise compare root.val with sum of root.left.val and root.right.val.
Edge Case Handling
Ensure the implementation correctly handles negative numbers and zero. This pattern highlights precise state tracking even in minimal binary trees.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
Time complexity is O(1) because the tree always has three nodes, and space complexity is O(1) for storing references to left and right children without recursion or extra structures.
What Interviewers Usually Probe
- Expect a single-step comparison without iteration.
- Ask about handling negative values and zero.
- Check if candidate considers unnecessary recursion and avoids it.
Common Pitfalls or Variants
Common pitfalls
- Assuming tree can have more nodes than specified.
- Forgetting to handle negative child values correctly.
- Using unnecessary recursion or loops for a fixed three-node tree.
Follow-up variants
- Check root equals sum of all descendant nodes.
- Extend to N-ary tree with sum of children check.
- Verify sum condition recursively for each subtree.
FAQ
What is the easiest way to solve Root Equals Sum of Children?
Directly compare root.val with left.val + right.val since the tree has exactly three nodes.
Does this problem allow negative node values?
Yes, nodes can have values from -100 to 100, so handle negatives correctly.
Should I use recursion for this problem?
Recursion is unnecessary because the tree only has three nodes, direct comparison is sufficient.
How does the binary-tree traversal pattern apply here?
Even with only three nodes, tracking left and right child state ensures accurate sum comparison.
What are common mistakes in implementing this solution?
Overcomplicating with loops or recursion, ignoring negative values, or assuming more nodes exist than specified.
Solution
Solution 1
#### Python3
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def checkTree(self, root: Optional[TreeNode]) -> bool:
return root.val == root.left.val + root.right.valContinue Topic
tree
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
Binary-tree traversal and state tracking
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward