Hide sidebar

Root Equals Sum of Children

Trees

Problem Statement

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

1046

Input: root = [10,4,6]

Output: true

The aue of the root is 10. The sum of its children is 4 + 6 = 10. 10 == 10, so we return true.

Algorithm Explanation

The solution is straightforward. We just need to check if the root's value is equal to the sum of its children's values.

Algorithm Steps

  • Access the root's value.
  • Access the left child's value.
  • Access the right child's value.
  • Return `true` if `root.val == root.left.val + root.right.val`, and `false` otherwise.
1046
Start
Checking if root value equals sum of children.
Root Equals Sum of Children Solution

class Solution:
    def checkTree(self, root: Optional[TreeNode]) -> bool:
        return root.val == root.left.val + root.right.val