Check if a binary tree is a sum tree, where each node equals the sum of its subtrees

Quick Overview

Decide whether every non-leaf node in a binary tree equals the total of all values in its left and right subtrees. It tests post-order traversal, passing subtree sums up the tree in one pass, and handling missing children and very deep trees.

Check if a binary tree is a sum tree, where each node equals the sum of its subtrees

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given the `root` of a binary tree, determine if the tree is a Sum Tree. A binary tree is a Sum Tree if, for every node, the node's value is equal to the sum of the values in its left subtree and right subtree. An empty tree is considered a Sum Tree with sum 0. A leaf node is also considered a valid Sum Tree. Return `True` if the binary tree is a Sum Tree, or `False` otherwise. ### Function Signature ```python def is_sum_tree(root: TreeNode | None) -> bool: ``` ### Rules - `root` is a `TreeNode` with fields `val`, `left` and `right`. The examples write the tree as a level-order list in which `None` marks a missing child. - A leaf (a node with no children) always passes. - Every other node must equal the sum of all values in its left subtree plus all values in its right subtree, counting whole subtrees and not only the two children. - A missing child is an empty subtree and contributes 0, so a node with one child must equal the sum of that child's subtree. ### Constraints - `1 <= number of nodes <= 10^4` - `-10^5 <= Node.val <= 10^5` ### Examples ```text Input: root = [26, 10, 3, 4, 6, 1, 2] Output: true ``` The leaves 4, 6, 1 and 2 pass, node 10 equals 4 + 6 and node 3 equals 1 + 2. The root's left subtree sums to 20 and its right subtree to 6, and 20 + 6 = 26, so every node passes. ```text Input: root = [10, 4, 6, 1, 2, 1, 2] Output: false ``` Node 4 has children 1 and 2, which sum to 3, and node 6 has the same two children. Either violation alone makes the answer false. ```text Input: root = [6, 3, None, 3] Output: true ``` The middle node 3 has only a left child 3, and its missing right child counts as 0, so it passes. The root's left subtree sums to 6 and its right subtree is empty, so the root passes too.

Overview: Decide whether every non-leaf node in a binary tree equals the total of all values in its left and right subtrees. It tests post-order traversal, passing subtree sums up the tree in one pass, and handling missing children and very deep trees.

Amazon logo
Amazon
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given the root of a binary tree, determine if the tree is a Sum Tree.

A binary tree is a Sum Tree if, for every node, the node's value is equal to the sum of the values in its left subtree and right subtree. An empty tree is considered a Sum Tree with sum 0. A leaf node is also considered a valid Sum Tree.

Return True if the binary tree is a Sum Tree, or False otherwise.

Function Signature

def is_sum_tree(root: TreeNode | None) -> bool:

Rules

  • root is a TreeNode with fields val , left and right . The examples write the tree as a level-order list in which None marks a missing child.
  • A leaf (a node with no children) always passes.
  • Every other node must equal the sum of all values in its left subtree plus all values in its right subtree, counting whole subtrees and not only the two children.
  • A missing child is an empty subtree and contributes 0, so a node with one child must equal the sum of that child's subtree.

Constraints

  • 1 <= number of nodes <= 10^4
  • -10^5 <= Node.val <= 10^5

Examples

Input: root = [26, 10, 3, 4, 6, 1, 2]
Output: true

The leaves 4, 6, 1 and 2 pass, node 10 equals 4 + 6 and node 3 equals 1 + 2. The root's left subtree sums to 20 and its right subtree to 6, and 20 + 6 = 26, so every node passes.

Input: root = [10, 4, 6, 1, 2, 1, 2]
Output: false

Node 4 has children 1 and 2, which sum to 3, and node 6 has the same two children. Either violation alone makes the answer false.

Input: root = [6, 3, None, 3]
Output: true

The middle node 3 has only a left child 3, and its missing right child counts as 0, so it passes. The root's left subtree sums to 6 and its right subtree is empty, so the root passes too.