Find all roots that minimize the height of a tree

Quick Overview

Given an undirected tree with n labeled nodes, return every node that gives the smallest possible height when the tree is rooted there. The problem tests adjacency-list graph modeling, reasoning about tree centers and eccentricity, and finding a linear-time method for trees with up to 20,000 nodes.

Find all roots that minimize the height of a tree

Company: Adobe

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

A company's departments form a tree, given as an undirected graph with `n` nodes labeled from `0` to `n - 1` and an array of `n - 1` edges, where `edges[i] = [a_i, b_i]` is an undirected edge between nodes `a_i` and `b_i`. Find the node (or nodes) that minimizes the maximum distance to all other nodes. These nodes are the center of the tree. Return the labels of all nodes that give the tree its minimum height when used as the root. ### Function Signature ```python def find_min_height_roots(n: int, edges: list[list[int]]) -> list[int]: ``` ### Rules - `edges[i] = [a, b]` is an undirected edge. The order of the two labels in a pair carries no meaning. - The height of the tree rooted at node `r` is the largest number of edges on the path from `r` to any node. A tree with a single node has height 0. - Return every node that achieves the minimum height, with the labels sorted in ascending order. ### Constraints - `1 <= n <= 2 * 10^4` - `edges.length == n - 1` - `0 <= a_i, b_i < n` - `a_i != b_i` - All the pairs `(a_i, b_i)` are distinct. - The input is guaranteed to be a tree. ### Examples ```text Input: n = 4, edges = [[1,0],[1,2],[1,3]] Output: [1] ``` Node 1 is adjacent to every other node, so rooting the tree at node 1 gives height 1. Rooting it at node 0, 2 or 3 gives height 2, because the other two outer nodes are two edges away. ```text Input: n = 6, edges = [[3,0],[3,1],[3,2],[3,4],[5,4]] Output: [3,4] ``` Rooted at node 3, the farthest node is 5 at distance 2. Rooted at node 4, the farthest nodes are 0, 1 and 2, also at distance 2. Every other root has some node three edges away (node 0 and node 5 are three edges apart), so nodes 3 and 4 are both centers. ```text Input: n = 1, edges = [] Output: [0] ``` A single node is the only possible root, and the tree has height 0.

Overview: Given an undirected tree with n labeled nodes, return every node that gives the smallest possible height when the tree is rooted there. The problem tests adjacency-list graph modeling, reasoning about tree centers and eccentricity, and finding a linear-time method for trees with up to 20,000 nodes.

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

A company's departments form a tree, given as an undirected graph with n nodes labeled from 0 to n - 1 and an array of n - 1 edges, where edges[i] = [a_i, b_i] is an undirected edge between nodes a_i and b_i.

Find the node (or nodes) that minimizes the maximum distance to all other nodes. These nodes are the center of the tree.

Return the labels of all nodes that give the tree its minimum height when used as the root.

Function Signature

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

Rules

  • edges[i] = [a, b] is an undirected edge. The order of the two labels in a pair carries no meaning.
  • The height of the tree rooted at node r is the largest number of edges on the path from r to any node. A tree with a single node has height 0.
  • Return every node that achieves the minimum height, with the labels sorted in ascending order.

Constraints

  • 1 <= n <= 2 * 10^4
  • edges.length == n - 1
  • 0 <= a_i, b_i < n
  • a_i != b_i
  • All the pairs (a_i, b_i) are distinct.
  • The input is guaranteed to be a tree.

Examples

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

Node 1 is adjacent to every other node, so rooting the tree at node 1 gives height 1. Rooting it at node 0, 2 or 3 gives height 2, because the other two outer nodes are two edges away.

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

Rooted at node 3, the farthest node is 5 at distance 2. Rooted at node 4, the farthest nodes are 0, 1 and 2, also at distance 2. Every other root has some node three edges away (node 0 and node 5 are three edges apart), so nodes 3 and 4 are both centers.

Input: n = 1, edges = []
Output: [0]

A single node is the only possible root, and the tree has height 0.