Compute every node's subtree size in a tree rooted at node 0 using BFS

Quick Overview

Given an undirected tree with n nodes rooted at node 0, return the size of every node's subtree. It tests turning an edge list into a rooted tree with breadth-first search and then combining child results from the leaves up to the root.

Compute every node's subtree size in a tree rooted at node 0 using BFS

Company: Rivian

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given an undirected tree with `n` nodes labeled from `0` to `n - 1` and an array `edges` where `edges[i] = [ai, bi]` indicates an edge between nodes `ai` and `bi`. The tree is rooted at node `0`. For each node, compute the number of nodes in its subtree (including itself). A node's subtree consists of the node and all of its descendants in the rooted tree. Return an array `answer` of length `n` where `answer[i]` is the subtree size of node `i`. Use breadth-first search to determine the tree structure starting from the root, then compute the subtree sizes. ### Function Signature ```python def subtree_count(n: int, edges: list[list[int]]) -> list[int]: ``` ### Rules - Edges are undirected, so `[ai, bi]` says nothing about which node is the parent. The parent of a node is its neighbour on the path to node `0`. - `answer` is indexed by node label, so `answer[0]` is always `n`. - When `n = 1`, `edges` is empty and the answer is `[1]`. ### Constraints - `1 <= n <= 10^4` - `edges.length == n - 1` - `0 <= ai, bi < n` - The input represents a valid tree. ### Examples ```text Input: n = 7, edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]] Output: [7,3,3,1,1,1,1] 0 / \ 1 2 / \ / \ 3 4 5 6 ``` Node 0 has all 7 nodes in its subtree. Node 1 has nodes {1, 3, 4} and node 2 has nodes {2, 5, 6}, so both have size 3, and the leaves 3, 4, 5 and 6 have size 1 each. ```text Input: n = 5, edges = [[0,1],[1,2],[2,3],[3,4]] Output: [5,4,3,2,1] 0 - 1 - 2 - 3 - 4 ``` This is a chain rooted at 0, so the subtree of node `i` is every node from `i` to 4, which is `5 - i` nodes. ```text Input: n = 4, edges = [[3,1],[1,0],[2,0]] Output: [4,2,1,1] ``` Rooted at 0, node 0 has children 1 and 2, and node 3 hangs below node 1 even though the edges list node 3 first. Node 1's subtree is {1, 3}, so its size is 2.

Overview: Given an undirected tree with n nodes rooted at node 0, return the size of every node's subtree. It tests turning an edge list into a rooted tree with breadth-first search and then combining child results from the leaves up to the root.

Rivian logo
Rivian
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given an undirected tree with n nodes labeled from 0 to n - 1 and an array edges where edges[i] = [ai, bi] indicates an edge between nodes ai and bi. The tree is rooted at node 0.

For each node, compute the number of nodes in its subtree (including itself). A node's subtree consists of the node and all of its descendants in the rooted tree.

Return an array answer of length n where answer[i] is the subtree size of node i. Use breadth-first search to determine the tree structure starting from the root, then compute the subtree sizes.

Function Signature

def subtree_count(n: int, edges: list[list[int]]) -> list[int]:

Rules

  • Edges are undirected, so [ai, bi] says nothing about which node is the parent. The parent of a node is its neighbour on the path to node 0 .
  • answer is indexed by node label, so answer[0] is always n .
  • When n = 1 , edges is empty and the answer is [1] .

Constraints

  • 1 <= n <= 10^4
  • edges.length == n - 1
  • 0 <= ai, bi < n
  • The input represents a valid tree.

Examples

Input: n = 7, edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]]
Output: [7,3,3,1,1,1,1]

        0
       / \
      1   2
     / \ / \
    3  4 5  6

Node 0 has all 7 nodes in its subtree. Node 1 has nodes {1, 3, 4} and node 2 has nodes {2, 5, 6}, so both have size 3, and the leaves 3, 4, 5 and 6 have size 1 each.

Input: n = 5, edges = [[0,1],[1,2],[2,3],[3,4]]
Output: [5,4,3,2,1]

0 - 1 - 2 - 3 - 4

This is a chain rooted at 0, so the subtree of node i is every node from i to 4, which is 5 - i nodes.

Input: n = 4, edges = [[3,1],[1,0],[2,0]]
Output: [4,2,1,1]

Rooted at 0, node 0 has children 1 and 2, and node 3 hangs below node 1 even though the edges list node 3 first. Node 1's subtree is {1, 3}, so its size is 2.