Root Equals Sum of Children - 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.

Input & Output

Example 1 — Root Equals Sum
$ Input: root = [10,4,6]
Output: true
💡 Note: Root value 10 equals the sum of its children: 4 + 6 = 10
Example 2 — Root Not Equal Sum
$ Input: root = [5,3,1]
Output: false
💡 Note: Root value 5 does not equal the sum of its children: 3 + 1 = 4 ≠ 5
Example 3 — Negative Values
$ Input: root = [1,1,0]
Output: true
💡 Note: Root value 1 equals the sum: 1 + 0 = 1

Constraints

  • The tree consists of exactly 3 nodes: root, left child, and right child
  • -100 ≤ Node.val ≤ 100

Visualization

Tap to expand
Root Equals Sum of Children INPUT Binary Tree (3 nodes) 10 root 4 left 6 right root = [10, 4, 6] Array representation ALGORITHM STEPS 1 Get Root Value root.val = 10 2 Get Left Child root.left.val = 4 3 Get Right Child root.right.val = 6 4 Compare Values root == left + right ? 10 == 4 + 6 10 == 10 OK - Equal! FINAL RESULT Verification Complete OK 10 = 4 + 6 Sum matches root! Output: true Root equals sum of children Key Insight: This is a simple O(1) operation - just compare root value with sum of left and right children. No null checks needed since problem guarantees exactly 3 nodes exist. return root.val == root.left.val + root.right.val; TutorialsPoint - Root Equals Sum of Children | Single Null Check with Comparison
Asked in
Microsoft 15 Amazon 12
26.4K Views
Medium Frequency
~5 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen