Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Roots of the tree having minimum height
MEDIUM
25 mins
29 upvotes
Trees
Graph
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Trees
-
-
Graph
-
-
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Roots of the tree having minimum height

Contributed by
Ambuj verma
Medium
yellow-spark
0/80
Avg time to solve 25 mins
Success Rate 75 %
Share
29 upvotes

Problem Statement

You are given an undirected Tree having 'N' nodes. The nodes are numbered from 1 to 'N'. Your task is to find the sorted list of all such nodes, such that rooting the tree on that node gives the minimum height possible of the given tree.

A tree is a connected acyclic graph. The height of a rooted tree is the maximum value of the distance between the root and all nodes of the tree.

Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1:
2
3
1 2 
2 3
4
1 2
3 1
4 1
Sample Output 1:
2
1
Explanation for Sample Input 1:
For the first test case, if we root the tree at Node 2, the height of the resultant tree is 1 which is the minimum possible height. 
For the second test case, if we root the tree at Node 1, the height of the resultant tree is 1 which is the minimum possible height. 
Sample Input 2:
2
4
1 3 
2 3
4 2
2 
1 2
Sample Output 2:
2 3
1 2
Reset Code
Full screen
Auto
copy-code
Console