MediumTreeDepth-First SearchBinary TreeUpdated Sep 2026

2265. Count Nodes Equal to Average of Subtree

Asked at Snowflake

Difficulty

Medium

Time

O(n)

Space

O(h)

Pattern

Tree

Companies

1

Problem

You are given the root of a binary tree. For every node, look at its subtree — that node plus all of its descendants. Compute the average value of that subtree, rounded down to an integer. Count how many nodes have a value equal to that rounded average, and return that count.

The average of n values is their sum divided by n, with the result floored. A leaf is a subtree of size 1, so its average is just its own value — every leaf always counts.

A simple way is to walk the tree and, for each node, gather all values below it. That is slow on a long chain of nodes. The fast way is one post-order DFS that returns each subtree's sum and size, so a parent can combine its two children in O(1).

Every node has its own subtree average

081424/6 = 4 ✓511/2 = 5 ✓6Node 4 averages its 6-node subtree to 4; node 5 averagesits 2-node subtree to 5.088/1 = 8 ✓1456Leaves 0, 1 and 6 each have average equal to their ownvalue, and node 8 does too.
For each node we sum its whole subtree, divide by the number of nodes, floor it, and check if it equals the node value.

Constraints

  • ●The number of nodes in the tree is in the range [1, 1000].
  • ●0 <= Node.val <= 1000

Examples

Example 1
Inputroot = [4,8,5,0,1,null,6]
Output5
WhyNode 4: sum 24, size 6, average 4 — matches. Node 5: sum 11, size 2, average 5 — matches. Nodes 0, 1 and 6 are leaves, so each average equals its own value — matches. Node 8 has average 8 (only itself) — matches too, but node 5's subtree already covers 5 and 6. Counting all matches gives 5.
Example 2
Inputroot = [1]
Output1
WhyThe tree is a single node. Its subtree sum is 1 and its size is 1, so the average is 1, which equals the node value.

Asked At

1 companies
CompanyDifficulty
SnowflakeMediumView all Snowflake questions →

How to Think About It

1

Computing the sum and size of every subtree separately is O(n²) on a skewed tree.

2

Key insight: a node's subtree sum is left sum + right sum + node.val and its size is left size + right size + 1. So return (sum, size) from a post-order DFS.

3

At each node, compare node.val with sum // size and increment a counter if they are equal.

4

Values are non-negative, so floor division behaves as expected in every language.

5

Walkthrough for a leaf: sum = its value, size 1, average = its value -> every leaf counts.

Optimal Approach

Step 1: count = 0.
Step 2: dfs(node) returns (sum, size):
Null -> (0, 0).
(ls, lc) = dfs(left), (rs, rc) = dfs(right).
s = ls + rs + node.val, c = lc + rc + 1.
If s // c == node.val, count += 1.
Return (s, c).
Step 3: Return count.

Time: O(n). Space: O(h).

Post-order DFS returns (sum, size)

0(0,1)81(1,1)456(6,1)Step 1 — leaves return (value, 1); each leaf matches itsown average, so count grows by 3.08(8,1) ✓145(11,2) ✓6Step 2 — node 8 returns (8,1); node 5 combines child 6:sum 5+6=11, size 1+1=2, and 11//2 = 5 matches.0814(24,6) ✓56Step 3 — the root adds both sides: sum 8+11+4 = 24, size1+2+3 = 6, and 24//6 = 4 matches.sum = left.sum + right.sum + node.valsize = left.size + right.size + 1count += 1 if sum // size == node.val
Children are solved first; each parent adds their sums and sizes, then compares its own value with sum // size.
ComplexityTime O(n)Space O(h)Each node is visited once and its sum/size are combined in constant time; h is the tree height, which bounds the recursion stack.

What Trips People Up in Real Interviews

1

Recomputing subtree sums for every node from scratch.

2

Rounding the average instead of flooring it.

3

Excluding the node itself from its own subtree average.

4

Using floating-point division and comparing doubles.

Solution Code

O(n) time
def averageOfSubtree(root):
    count = 0

    def dfs(node):
        nonlocal count
        if not node:
            return 0, 0
        ls, lc = dfs(node.left)
        rs, rc = dfs(node.right)
        s = ls + rs + node.val
        c = lc + rc + 1
        if s // c == node.val:
            count += 1
        return s, c

    dfs(root)
    return count

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Count Nodes Equal to Average of Subtree problem?

Count Nodes Equal to Average of Subtree asks how many nodes have a value equal to the (floored) average of all values in their subtree, including themselves. A single post-order traversal that returns each subtree's sum and size answers it in linear time.

How do you solve Count Nodes Equal to Average of Subtree?

The optimal approach runs in O(n) time and O(h) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Count Nodes Equal to Average of Subtree?

Count Nodes Equal to Average of Subtree is asked at Snowflake. It is a medium difficulty problem.

What are common mistakes on Count Nodes Equal to Average of Subtree?
  • Recomputing subtree sums for every node from scratch.
  • Rounding the average instead of flooring it.
  • Excluding the node itself from its own subtree average.
  • Using floating-point division and comparing doubles.