CMD Guide
HomeDSACompany Practice

easy Root Equals Sum of Children

Problem Statement

Given a binary tree with exactly 3 nodes (single root and two child nodes), determine if the root's value is equal to the sum of the child nodes values.

Examples

Try it yourself

Try solving this question here:

🎯 STRICT STANDOUT — Root Equals Sum of Children — problem

1. Why / judgment

Local equality check: parent value equals sum of its children values. On the common 3-node constrained version, only root/left/right exist — O(1). Pattern is still “local tree property” which scales to validate-all-nodes variants via DFS.

2. Big-O derivation (K11)

O(1) for fixed 3-node; O(n) if every non-leaf must satisfy.
Space O(1) / O(h).

3. Pattern + when-NOT (K12)

Name: LOCAL PARENT-CHILDREN INVARIANT

Recognition: value equals sum of children.

When-NOT: Need sum of whole subtree → different aggregation (see avg-of-subtree). Need BST property → ordering constraints, not sum.

4. Edge hand-run (K13)

missing child treat as 0 if problem says so; LC 2236 both children exist.
negative values allowed per constraints — sum still works.

5. Interviewer follow-ups (model answers)

Q1. Why is this interview-easy?
A: Warm-up for tree node field access and null rules; pattern expands to subtree sums.

Q2. Integer overflow?
A: Use wider type if values large.

Q3. Recursive overkill?
A: For single root check yes; for whole-tree validation recursion is natural.

6. Short drills

Drill: root=10 left=4 right=6 → true.
Drill: extend to “every node equals sum of children”.
✅ Solution Root Equals Sum of Children

Problem Statement

Given a binary tree with exactly 3 nodes (single root and two child nodes), determine if the root's value is equal to the sum of the child nodes values.

Examples

  • Example 1:

    • Input: root = [10, 3, 7]
    • Expected Output: true
    • Justification: The root's value (10) is equal to the sum of its children's values (3 + 7).
  • Example 2:

    • Input: root = [4, 1, 5]
    • Expected Output: false
    • Justification: The root's value (4) is not equal to the sum of its children's values (1 + 5).
  • Example 3:

    • Input: root = [15, 10, 5]
    • Expected Output: true
    • Justification: The root's value (15) is equal to the sum of its children's values (10 + 5).

Solution

To solve this problem, we'll perform a straightforward comparison between the root node's value and the sum of its children's values. This approach is effective because the problem's scope is limited to a root with only two children, simplifying the verification process. By directly accessing the root and its children's values, we can determine the truth of the statement with minimal computation, making this method both efficient and straightforward.

This approach ensures accuracy and performance by eliminating the need for traversing or searching through additional nodes, as the problem guarantees the presence of exactly two children for the root node.

Step-by-step Algorithm

  • Step 1: Access the value of the root node.
  • Step 2: Access the values of the root's two children nodes.
  • Step 3: Sum the values of the two children nodes.
  • Step 4: Compare the sum of the children's values with the root's value.
  • Step 5: Return true if the root's value equals the sum of its children's values; otherwise, return false.

Algorithm Walkthrough

Given the input root = [10, 3, 7]:

  • Step 1: The root's value is 10.
  • Step 2: The first child's value is 3, and the second child's value is 7.
  • Step 3: The sum of the children's values is 3 + 7 = 10.
  • Step 4: Compare the root's value (10) with the sum of its children's values (10).
  • Step 5: Since the root's value equals the sum of its children's values, return true.

Code

java
// class TreeNode {
//     int val;
//     TreeNode left;
//     TreeNode right;
//     TreeNode(int x) { val = x; }
//     TreeNode(int x, TreeNode left, TreeNode right) {
//         this.val = x;
//         this.left = left;
//         this.right = right;
//     }
// }

class Solution {

  // Method to check if root's value equals the sum of its children
  public boolean checkTree(TreeNode root) {
    // Check if the root's value is equal to the sum of its children's values
    return root.val == root.left.val + root.right.val;
  }

  public static void main(String[] args) {
    Solution solution = new Solution();

    // Example 1
    TreeNode example1 = new TreeNode(10, new TreeNode(3), new TreeNode(7));
    System.out.println(solution.checkTree(example1)); // Expected: true

    // Corrected Example 2
    TreeNode example2 = new TreeNode(4, new TreeNode(1), new TreeNode(5));
    System.out.println(solution.checkTree(example2)); // Expected: false

    // Example 3
    TreeNode example3 = new TreeNode(15, new TreeNode(10), new TreeNode(5));
    System.out.println(solution.checkTree(example3)); // Expected: true
  }
}

Complexity Analysis

Time Complexity

: The algorithm performs a constant time operation, as it directly accesses the root and its two children without traversing any additional nodes. This means the execution time remains constant regardless of the input size.

Space Complexity

: The space complexity is also constant because the algorithm uses a fixed amount of space. It does not allocate any extra data structures that grow with the input size.

🎯 STRICT STANDOUT — Solution Root Equals Sum of Children

1. Why / judgment

Read three values; compare. State null policy explicitly if a child may be absent. No traversal needed for the 3-node problem — don’t over-engineer DFS.

2. Big-O derivation (K11)

Θ(1) time/space for 3-node.
If generalized full validation: Θ(n).

3. Pattern + when-NOT (K12)

Name: DIRECT LOCAL CHECK

Recognition: root equals children sum.

When-NOT: Subtree sum equality → postorder aggregate.

4. Edge hand-run (K13)

(0,0,0) true.
(1,1,0) true if 0 child allowed.
(5,2,2) false.

5. Interviewer follow-ups (model answers)

Q1. Why mention pattern if O(1)?
A: Company sets chain easy→medium: next question often “all nodes” or “average of subtree”.

Q2. Floating children?
A: Integers only in standard statement.

Q3. Multiple children N-ary?
A: Sum all children list.

6. Short drills

Drill: write both 3-node and full-tree validators.
Drill: contrast with path-sum problems.
🧩 Pattern · Arrays

Recognize it: Scan once tracking what you need (running max/sum), or precompute a prefix-sum / hash → turn O(n²) into O(n).

▶ Visualize this problem (step it, predict each fork)
⛶ Open this problem debugger in explore mode
🤖 Don't fully get this? Learn it with Claude

Stuck on Root Equals Sum of Children? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🪜 Hint ladder (no spoilers)

Progressively stronger hints — you still solve it.

I'm working on the problem **Root Equals Sum of Children** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🎨 Explain the approach visually

See the technique, not just code.

Explain the optimal approach to **Root Equals Sum of Children** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔍 Review my solution

Catch bugs, edge cases, sub-optimality.

I'll paste my solution to **Root Equals Sum of Children**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔁 Drill the pattern

Lock in recognition with look-alikes.

Give me 2 problems that use the SAME underlying pattern as **Root Equals Sum of Children**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes