Root Equals Sum of Children
EasyPrompt
You are given the root of a binary tree that consists of exactly 3 nodes: the root, its left child, and its right child.
Return true if the value of the root is equal to the sum of the values of its two children, or false otherwise.
Example 1:
Input: root = [10,4,6]
Output: true
Explanation: 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
Explanation: 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
Approaches
2 approaches with complexity analysis and trade-offs.
This method employs a standard iterative tree traversal technique, specifically Breadth-First Search (BFS), using a queue. We add the root's children to a queue, then process the queue to sum their values. This approach is functionally correct but is considered inefficient for this problem due to its unnecessary complexity and overhead.
Algorithm
- Initialize a
sumvariable to 0. - Create a
Queueto store the children of the root. - Add the left and right children to the queue. The problem statement guarantees they exist.
- Loop while the queue is not empty:
- Dequeue a node.
- Add the node's value to the
sum.
- After the loop, compare the
sumwith the root's value and return the result.
Walkthrough
The algorithm works as follows:
- A queue is initialized to hold tree nodes.
- The left and right children of the root are added to the queue. Since the problem guarantees these children exist, we don't need null checks.
- A variable
childrenSumis initialized to 0. - We then loop until the queue is empty, dequeuing each child and adding its value to
childrenSum. - Finally, we compare
root.valwithchildrenSumand return the boolean result. While this is a robust method for general tree traversal, it's overkill for a fixed 3-node tree.
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */import java.util.LinkedList;import java.util.Queue; class Solution { public boolean checkTree(TreeNode root) { if (root == null) { return false; } Queue<TreeNode> children = new LinkedList<>(); // The problem guarantees the children exist. children.add(root.left); children.add(root.right); int sum = 0; while(!children.isEmpty()) { TreeNode child = children.poll(); sum += child.val; } return root.val == sum; }}Complexity
Time
O(1). The number of operations is constant as the tree size is fixed at 3. We perform a fixed number of additions to the queue and the loop runs exactly twice.
Space
O(1). The queue stores at most two elements, so the space used is constant and does not depend on the input values.
Trade-offs
Pros
It's a generic approach that demonstrates a standard tree traversal algorithm (BFS).
Cons
Introduces unnecessary overhead from using a
Queuedata structure.The code is more verbose and less clear than a direct solution.
It's computationally slower in practice due to method calls for queue operations, even though the Big O complexity is the same.
This is the most straightforward and optimal approach. Given the problem's constraint that the tree consists of exactly three nodes (a root and its two children), we can directly access the values of all three nodes and perform the required check in a single line.
Algorithm
- Access the value of the root node (
root.val). - Access the value of the left child (
root.left.val). - Access the value of the right child (
root.right.val). - Return the result of the boolean comparison:
root.val == root.left.val + root.right.val.
Walkthrough
The logic is extremely simple:
- Access the value of the root node via
root.val. - Access the value of the left child via
root.left.val. - Access the value of the right child via
root.right.val. - Sum the values of the two children.
- Compare this sum directly with the root's value and return the boolean result of this comparison. This avoids any loops, recursion, or auxiliary data structures, making it the most efficient solution in terms of both time and space, as well as code simplicity.
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */class Solution { public boolean checkTree(TreeNode root) { return root.val == root.left.val + root.right.val; }}Complexity
Time
O(1). The solution involves a fixed number of memory accesses, one addition, and one comparison, which are all constant time operations.
Space
O(1). No extra space is allocated that depends on the input. The operations are performed in place.
Trade-offs
Pros
Most efficient in terms of both time and space.
Extremely simple, concise, and easy to read and understand.
Directly solves the problem without any unnecessary abstractions or overhead.
Cons
This solution is highly specific to the problem's constraints (a 3-node tree) and is not a general-purpose solution for other tree problems.
Solutions
Solution
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public boolean checkTree ( TreeNode root ) { return root . val == root . left . val + root . right . val ; } }/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: bool checkTree ( TreeNode * root ) { return root -> val == root -> left -> val + root -> right -> val ; } };# 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 . valVideo walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.