← DiffPush

Check Children Sum Property (Sum Tree)

DiffPush Tier Binary Trees · Hard O(n) · O(h)

The Ledger Roll-Up Verification

A finance bot keeps its books as a binary tree where each account node should hold exactly the total rolling up from everything beneath it — the node's own balance must equal the combined totals of its two sub-ledgers. Before the quarter closes, the auditor asks a single yes/no question: does every non-leaf balance reconcile with the roll-up beneath it?

Input: A binary tree as a level-order array (null marks a missing child), given by its root.

Output: Return true if every non-leaf node's value equals the sum of its left and right subtree totals; otherwise return false.

Constraints

Examples

Example 1

Input: {"tree":[3,1,2]}
Output: true
The root's 3 is exactly 1 + 2, and both children are leaves that need no check.

Example 2

Input: {"tree":[26,10,3,4,6,null,3]}
Output: true
26 rolls up 20 (from the 10-4-6 wing) plus 6 (from the 3-3 wing); every internal node reconciles.

Solve this in your browser →

Also on LeetCode ↗