Skip to main content
Back to problems
#2236
Easy Algorithms

Root equals sum of children

Tree Binary Tree
84.9% acceptance
Feb 25, 2026
1564
1646
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.

Solution

Rust
Time O(1)
Space O(1)
LeetCode
solution.rs
use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
  pub fn check_tree(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
    let root = root.unwrap();
    let root = root.borrow();
    let left = root.left.as_ref().unwrap().borrow().val;
    let right = root.right.as_ref().unwrap().borrow().val;
    root.val == left + right
  }
}