↓Skip to main content
  1. LeetCode/

LeetCode 2236: Root Equals Sum of Children

·1 min· ·
LeetCode Easy Tree Binary-Tree
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2025-12-17

  • Submissions: Python Accepted → C++ Compile Error → C++ Accepted

Approach
#

The tree is guaranteed to have only a root and two children, so compare root.val with the sum of the two children.

My Attempts
#

Python passed on the first try. The C++ rewrite first failed to compile because of a missing semicolon:

return (root->left->val + root->right->val) == root->val    // missing ;

Solution
#

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

Complexity
#

Time O(1), space O(1).

Mistakes to Avoid
#

  • In C++, access members through a pointer with ->, not ..
  • A C++ return statement needs a trailing semicolon.

Interview Explanation
#

The tree always has exactly three nodes, so I compare the root value with the sum of its two children. Time and space are O(1).

Related

LeetCode 1022: Sum of Root To Leaf Binary Numbers
·2 mins
LeetCode Easy Tree Depth-First-Search Binary-Tree
LeetCode 1022 note: on the way down double the current number and add the node’s bit, adding it to the total at a leaf
LeetCode 938: Range Sum of BST
·2 mins
LeetCode Easy Tree Depth-First-Search Binary-Search-Tree Binary-Tree
LeetCode 938 note: prune with the BST order, going only right below low and only left above high, adding nodes inside the range
LeetCode 965: Univalued Binary Tree
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 965 note: every value must equal the root’s; compare values while recursing, not the booleans the recursion returns
LeetCode 993: Cousins in Binary Tree
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 993 note: cousins share a depth but not a parent; record x’s and y’s depth and parent during DFS and compare
LeetCode 872: Leaf-Similar Trees
·1 min
LeetCode Easy Tree Depth-First-Search Binary-Tree
LeetCode 872 note: collect leaves left to right with DFS; BFS collects level by level, which is not left-to-right order
LeetCode 637: Average of Levels in Binary Tree
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 637 note: level-order traversal, dividing each level’s sum by its count