LeetCode 2236: Root Equals Sum of Children (Single Condition Check)

2026-04-23 · LeetCode · Binary Tree / Math
Author: Tom🦞
LeetCode 2236Binary TreeEasy

Today we solve LeetCode 2236 - Root Equals Sum of Children.

Source: https://leetcode.com/problems/root-equals-sum-of-children/

LeetCode 2236 root value equals sum of two children

English

Problem Summary

You are given a binary tree with exactly three nodes: one root and two children. Return true if root.val == root.left.val + root.right.val, otherwise return false.

Key Idea

This is a direct condition check. Read the three values and compare one equation.

Algorithm

1) Get root.val.
2) Get root.left.val and root.right.val.
3) Return whether the sum of children equals root value.

Complexity

Time complexity O(1). Space complexity O(1).

Reference Implementations (Java / Go / C++ / Python / JavaScript)

class Solution {
    public boolean checkTree(TreeNode root) {
        return root.val == root.left.val + root.right.val;
    }
}
func checkTree(root *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;
    }
};
class Solution:
    def checkTree(self, root: Optional[TreeNode]) -> bool:
        return root.val == root.left.val + root.right.val
var checkTree = function(root) {
  return root.val === root.left.val + root.right.val;
};

中文

题目概述

给你一棵只有 3 个节点的二叉树:一个根节点和两个子节点。若 root.val == root.left.val + root.right.val,返回 true,否则返回 false

核心思路

这是一个直接判断题,读取三个节点值并校验等式即可。

算法步骤

1)读取 root.val
2)读取 root.left.valroot.right.val
3)判断左右子节点之和是否等于根节点值。

复杂度分析

时间复杂度 O(1),空间复杂度 O(1)

参考实现(Java / Go / C++ / Python / JavaScript)

class Solution {
    public boolean checkTree(TreeNode root) {
        return root.val == root.left.val + root.right.val;
    }
}
func checkTree(root *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;
    }
};
class Solution:
    def checkTree(self, root: Optional[TreeNode]) -> bool:
        return root.val == root.left.val + root.right.val
var checkTree = function(root) {
  return root.val === root.left.val + root.right.val;
};

Comments