Minimum Height Trees

Algorithms
Medium
Salesforce
130.6K views

A tree is an undirected graph in which any two vertices are connected by exactly one path. Find all Minimum Height Trees (MHTs) and return the roots of those MHTs. Use a graph peeling technique (BFS).

Why Interviewers Ask This

Salesforce interviewers ask this to evaluate your ability to optimize graph traversal beyond standard BFS. They specifically test if you recognize that the center of a tree minimizes height, rather than brute-forcing every node. This assesses your skill in identifying edge cases, understanding topological sorting properties, and writing efficient O(N) solutions under pressure.

How to Answer This Question

1. Clarify constraints: Confirm if the graph is connected and handle edge cases like single-node inputs immediately. 2. Visualize the problem: Explain that MHT roots are the 'centers' of the tree, meaning they minimize the maximum distance to any leaf. 3. Propose the algorithm: Introduce the topological peeling approach where you iteratively remove leaf nodes (degree 1) until one or two nodes remain. 4. Walk through logic: Detail how you initialize a queue with all leaves, decrement neighbor degrees, and add new leaves to the queue in each iteration. 5. Analyze complexity: Explicitly state that time complexity is O(V+E) since each node and edge is processed once, proving efficiency over running BFS from every node.

Key Points to Cover

  • Recognizing that the solution requires finding the graph center, not just any path
  • Implementing the topological sort peeling technique for O(N) efficiency
  • Handling edge cases like single nodes or disconnected components correctly
  • Explaining why removing leaves layer-by-layer guarantees the minimum height
  • Demonstrating clear communication of the iterative reduction process

Sample Answer

To solve the Minimum Height Trees problem efficiently, I would avoid running a full BFS from every single node, as that results in O(N^2) complexity. Instead, I would use a topological peeling strategy which runs in linear time. First, I'd handle base cases where the number of nodes is one or two, returning them directly. For larger graphs, I calculate the degree of every node and initialize a queue with all current leaf nodes, which have a degree of one. Then, I enter a loop where I process the current layer of leaves. As I remove a leaf, I decrement the degree of its neighbors. If a neighbor's degree drops to one, it becomes a new leaf for the next iteration. I repeat this process, peeling away layers from the outside in, until only one or two nodes remain in the queue. These final nodes are the roots of the minimum height trees because they represent the geometric center of the graph. This approach ensures we find the optimal root without redundant traversals, aligning with Salesforce's focus on scalable, high-performance data processing systems.

Common Mistakes to Avoid

  • Running BFS from every node individually, leading to unnecessary O(N^2) time complexity
  • Failing to handle the edge case where the input contains only one or two nodes
  • Using DFS instead of BFS/Topological Sort, which complicates the layer-peeling logic
  • Not explaining why the last remaining nodes are guaranteed to be the optimal roots

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 145 Algorithms questionsBrowse all 49 Salesforce questions