Pick Root Nodes That Minimize the Height of a Network Tree
Last updated: November 29, 2026
Quick Overview
Given an undirected tree of n nodes, return every node that, when chosen as the root, produces a tree of minimum height.
0
0
0 solved
Given an undirected tree of n nodes, return every node that, when chosen as the root, produces a tree of minimum height.
An onsite question that feels like choosing where to place a coordinator in a tree-shaped network so the worst-case hop count is smallest. The interviewer wants to see you avoid running BFS from every node and instead peel leaves layer by layer toward the center.
What the Interviewer Expects
- Recognize that BFS from every node is O(n^2)
- Peel leaves layer by layer like a topological sort
- Explain why at most two centers exist
- Handle n equal to 1 and n equal to 2
- Relate the answer to the middle of the tree's diameter
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Prove that there are at most two centers.
- Find the centers using two BFS passes for the diameter instead.
- What if edges have weights?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input:
n,edgesforming a tree. - Output: list of 1 or 2 node labels.
- n can be large (tens of thousands), so quadratic is too slow....
High-Level Design
The height from a root is its eccentricity, the distance to the farthest node. The nodes of minimum eccentricity are the center of the tree, which is the middle one or two nodes of any longest path (d...