Minimum Height Trees - Complete Solution Guide
Minimum Height Trees is LeetCode problem 310, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
A tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any connected graph without simple cycles is a tree. Given a tree of n nodes labelled from 0 to n - 1 , and an array of n - 1 edges where edges[i] = [a i , b i ] indicates that there is an undirected edge between the two nodes a i and b i in the tree, you can choose any node of the tree as the root. When you select a node x as the root, the result tree has height h . Among all possible root
Detailed Explanation
The problem asks us to find the minimum height trees (MHTs) of a given undirected tree. We are given the number of nodes `n` and a list of edges `edges` representing the tree. We need to return a list of the root labels of all possible MHTs. The height of a rooted tree is defined as the number of edges on the longest path from the root to a leaf. An MHT is a tree with the minimum possible height among all possible rooted trees formed by choosing different nodes as the root.
Solution Approach
The provided solution uses a topological sort-like approach, akin to a breadth-first search (BFS), to iteratively remove leaf nodes. It starts by identifying all leaf nodes in the tree and placing them in a queue. Then, it repeatedly removes leaf nodes from the queue and updates the degrees of their neighbors. If a neighbor's degree becomes 1 after removing a leaf, it is added to the queue. This process continues until only two or fewer nodes remain. These remaining nodes are the roots of the MHTs.
Step-by-Step Algorithm
- Step 1: Handle the base case where n = 1. In this case, the only node (0) is the root of the MHT.
- Step 2: Build an adjacency list (`adj`) to represent the graph, and an array `degrees` to store the degree of each node.
- Step 3: Initialize a queue `leaves` with all nodes that have a degree of 1 (leaf nodes).
- Step 4: While the number of remaining nodes (`remaining_nodes`) is greater than 2:
- a. Remove all current leaf nodes from the `leaves` queue.
- b. For each removed leaf node, decrement the degree of its neighbors.
- c. If a neighbor's degree becomes 1 after decrementing, add it to the `leaves` queue.
- d. Update `remaining_nodes` by subtracting the number of removed leaves.
- Step 5: The remaining nodes in the `leaves` queue are the roots of the MHTs. Return them as a list.
Key Insights
- Insight 1: The MHTs are essentially the middle nodes of the longest path in the tree. If the longest path has an odd number of nodes, there is one middle node. If it has an even number of nodes, there are two middle nodes.
- Insight 2: Using a topological sort approach, we can iteratively remove leaf nodes (nodes with degree 1) until only the middle node(s) remain. These remaining nodes will be the roots of the MHTs.
- Insight 3: The algorithm simulates pruning the tree from the leaves, layer by layer, until we reach the center(s). This is analogous to peeling an onion; the last layer(s) left are the core(s) we want.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Depth-First Search, Breadth-First Search, Graph, Topological Sort.
Companies
Asked at: Citadel, PhonePe, Splunk, Stackline.