Leetcode 2236: Root Equals Sum of Children

grid47
grid47
Exploring patterns and algorithms
Mar 28, 2024 4 min read

You are given a binary tree with three nodes: the root, its left child, and its right child. Determine whether the value of the root node is equal to the sum of the values of its two children.
Problem
Approach
Steps
Complexity
Input: The input consists of a binary tree with exactly three nodes: a root node, a left child, and a right child. Each node contains an integer value.
Example: Input: root = [7, 3, 4]
Constraints:
• The binary tree always has three nodes: root, left child, and right child.
• -100 <= Node.val <= 100
Output: Return a boolean value indicating whether the value of the root node is equal to the sum of the values of its two children.
Example: Output: true
Constraints:
• The output should be either true or false.
Goal: Check if the sum of the values of the left and right children equals the value of the root node.
Steps:
• 1. Access the value of the root node.
• 2. Access the values of the left and right child nodes.
• 3. Compute the sum of the left and right child node values.
• 4. Compare the computed sum with the value of the root node.
• 5. Return true if they are equal; otherwise, return false.
Goal: The binary tree structure is fixed with exactly three nodes, and all node values fall within the specified range.
Steps:
• The tree will always consist of three nodes: root, left child, and right child.
• Node values are integers within the range -100 to 100.
Assumptions:
• The binary tree input will always be valid with three nodes.
• No additional nodes or structures are present in the input.
Input: Input: root = [7, 3, 4]
Explanation: The value of the root is 7, and the sum of the left and right children is 3 + 4 = 7. Since they are equal, the output is true.

Input: Input: root = [8, 5, 2]
Explanation: The value of the root is 8, and the sum of the left and right children is 5 + 2 = 7. Since they are not equal, the output is false.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus