# Root Equals Sum of Children
**Difficulty:** EASY
[External](https://leetcode.com/problems/root-equals-sum-of-children)
Canonical: https://scaleengineer.com/dsa/problems/root-equals-sum-of-children
**Data structures:** Tree, Binary Tree
---
## Problem
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:**

![](https://assets.glich.co/dsa/root-equals-sum-of-children/image0.png) 

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

![](https://assets.glich.co/dsa/root-equals-sum-of-children/image1.png) 

**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
## Iterative Traversal using a Queue
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.
**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.
**Pros:** It's a generic approach that demonstrates a standard tree traversal algorithm (BFS).
**Cons:** Introduces unnecessary overhead from using a `Queue` data 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.
### Explanation
The algorithm works as follows:
1.  A queue is initialized to hold tree nodes.
2.  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.
3.  A variable `childrenSum` is initialized to 0.
4.  We then loop until the queue is empty, dequeuing each child and adding its value to `childrenSum`.
5.  Finally, we compare `root.val` with `childrenSum` and return the boolean result.
While this is a robust method for general tree traversal, it's overkill for a fixed 3-node tree.
```java
/**
 * 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;
    }
}
```
### Algorithm
- Initialize a `sum` variable to 0.
- Create a `Queue` to 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 `sum` with the root's value and return the result.

## Direct Sum and Comparison
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.
**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.
**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.
### Explanation
The logic is extremely simple:
1.  Access the value of the root node via `root.val`.
2.  Access the value of the left child via `root.left.val`.
3.  Access the value of the right child via `root.right.val`.
4.  Sum the values of the two children.
5.  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.
```java
/**
 * 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;
    }
}
```
### 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`.

# Solutions
### Java

```java
/** * 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 ; } }
```

### CPP

```cpp
/** * 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 ; } };
```

### Python

```python
# 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 . val
```
