Count the subtrees of a binary tree whose nodes all share one value

Quick Overview

Count how many subtrees of a binary tree, given as a level-order list, have every node holding the same value. It tests deciding each node from both of its children, handling nodes with one child and the empty tree, and keeping the work linear in the number of nodes.

Count the subtrees of a binary tree whose nodes all share one value

Company: eBay

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a binary tree, return the number of uni-value subtrees. A uni-value subtree is the subtree rooted at some node (that node and all of its descendants) in which every node has the same value. ### Function Signature ```python def count_univalue_subtrees(values: list[int | None]) -> int: ... ``` ### Rules - The tree is given as a level-order list in which `None` marks a missing child. The first entry is the root. After it, each existing node's left child and then its right child are listed, taking the nodes in the order they appear. Missing nodes have no children listed, and trailing `None` entries may be left out. An empty list is the empty tree. - Every node is the root of exactly one subtree, so the answer is the number of nodes whose whole subtree holds a single value. A leaf always counts. - The empty tree has no subtrees, so its answer is 0. ### Constraints - The number of nodes is in the range `[0, 1000]`. - `-1000 <= value <= 1000` ### Examples ```text Input: values = [5, 1, 5, 5, 5, None, 5] Output: 4 ``` The root 5 has children 1 and 5. Node 1 has two leaf children valued 5, and the root's right child has one right child valued 5. The three leaves count, and so does the root's right child (5 over 5), while the subtrees rooted at 1 and at the root mix values. ```text Input: values = [] Output: 0 ``` An empty tree has no subtrees. ```text Input: values = [5, 5, 5, 5, 5, None, 5] Output: 6 ``` The tree has the same shape as in the first example, but all six nodes hold 5, so every node's subtree is uni-value.

Overview: Count how many subtrees of a binary tree, given as a level-order list, have every node holding the same value. It tests deciding each node from both of its children, handling nodes with one child and the empty tree, and keeping the work linear in the number of nodes.

eBay logo
eBay
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a binary tree, return the number of uni-value subtrees. A uni-value subtree is the subtree rooted at some node (that node and all of its descendants) in which every node has the same value.

Function Signature

def count_univalue_subtrees(values: list[int | None]) -> int:
    ...

Rules

  • The tree is given as a level-order list in which None marks a missing child. The first entry is the root. After it, each existing node's left child and then its right child are listed, taking the nodes in the order they appear. Missing nodes have no children listed, and trailing None entries may be left out. An empty list is the empty tree.
  • Every node is the root of exactly one subtree, so the answer is the number of nodes whose whole subtree holds a single value. A leaf always counts.
  • The empty tree has no subtrees, so its answer is 0.

Constraints

  • The number of nodes is in the range [0, 1000] .
  • -1000 <= value <= 1000

Examples

Input: values = [5, 1, 5, 5, 5, None, 5]
Output: 4

The root 5 has children 1 and 5. Node 1 has two leaf children valued 5, and the root's right child has one right child valued 5. The three leaves count, and so does the root's right child (5 over 5), while the subtrees rooted at 1 and at the root mix values.

Input: values = []
Output: 0

An empty tree has no subtrees.

Input: values = [5, 5, 5, 5, 5, None, 5]
Output: 6

The tree has the same shape as in the first example, but all six nodes hold 5, so every node's subtree is uni-value.