File: root-equals-sum-of-children/solution.py

Date: 2026-06-06

Time: 18:57

Purpose

This file is the self-contained solution for LeetCode 2236: Root Equals Sum of Children. It owns three things: the TreeNode data structure, the Solution.checkTree algorithm, and the test suite that validates it. It's one of the simplest problems in the repository — a guaranteed 3-node binary tree where you check if root.val == left.val + right.val.

Key Components

TreeNode (lines 9–14)

Standard binary tree node with val, left, and right. This is the local definition used by both the solution and tests — it mirrors LeetCode's provided definition rather than importing a shared one.

Solution.checkTree (lines 18–26)

Single-expression method: return root.val == root.left.val + root.right.val. No null-checking, no recursion, no edge-case handling — the problem guarantees exactly 3 nodes with both children present.

TestCheckTree (lines 29–48)

Seven test cases covering:

The helper maketree constructs the fixed 3-node tree from three ints, keeping test setup DRY.

Patterns

Dependencies

Imports: annotations (PEP 604-style type hints), unittest, Optional from typing. All stdlib — no external packages.

Imported by: The "Imported By" list is enormous (~400+ test files), which almost certainly means those test files import TreeNode from this module as a shared fixture — not Solution or checkTree. This file is effectively the canonical TreeNode definition for the entire repository's test infrastructure.

Flow

1. Caller passes a TreeNode root guaranteed to have both .left and .right.

2. checkTree reads three .val attributes, computes one addition, one comparison.

3. Returns a bool. O(1) time, O(1) space.

Invariants

Error Handling

None. The method trusts the caller to satisfy the precondition (3-node tree). A None root or missing child causes an unhandled AttributeError. This is appropriate given LeetCode's guarantees.

Topics to Explore

Beliefs