We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Root Equals Sum of Children

Number: 2384

Difficulty: Easy

Paid? No

Companies: Google, Microsoft, Amazon, Meta


Problem Description

Given a binary tree consisting of exactly 3 nodes — the root, its left child, and its right child — determine if the value of the root is equal to the sum of the values of its two children.


Key Insights

  • The tree is very small with only 3 nodes.
  • Directly access the left and right children of the root.
  • Compare root.val with left.val + right.val.
  • Handle negative values since node values can be between -100 and 100.

Space and Time Complexity

Time Complexity: O(1)
Space Complexity: O(1)


Solution

Since the tree contains exactly 3 nodes, the solution is straightforward. Access the left and right child nodes of the root and sum their values. If the sum equals the root's value, return true; otherwise, return false. This solution uses a simple comparison without any need for recursion or iterative traversal, which is efficient given the fixed tree size.


Code Solutions

# 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: TreeNode) -> bool:
        # Check if the sum of the left and right child's value equals the root's value.
        return root.val == root.left.val + root.right.val

# Example Usage:
# root = TreeNode(10, TreeNode(4), TreeNode(6))
# print(Solution().checkTree(root))  # Output: True
← Back to All Questions