Given a N-ary tree with N nodes numbered from 0 to N-1 and a list of undirected edges, the task is to find the node(s) at the centre of the given tree.
Eccentricity: The eccentricity of any vertex V in a given tree is the maximum distance between the given vertex V and any other vertex of the tree.
Centre: The centre of a tree is the vertex having the minimum eccentricity. Hence, it means that in order to find the centre we have to minimise this eccentricity.
Approach: It can be observed that the path of maximum eccentricity is the diameter of the tree. Hence, the centre of the tree diameter will be the centre of the tree as well.
- For example, Let’s consider a case where the longest path consists of odd number of vertices. Let the longest path be X —— O ——– Y where X and Y are the two endpoints of the path and O is the middle vertex.
- For a contradiction, if the centre of the tree is not O but some other vertex O’, then at least one of the following two statements must be true.
- Path XO’ is strictly longer than path XO
- Path YO’ is strictly longer than path YO
- This means O’ will not satisfy the condition of minimum eccentricity. Hence by contradiction, we have proved that the centre of the tree is actually the centre of the diameter path.
- Now if the diameter consists odd number of nodes, then there exists only 1 centre (also known as Central Tree).
- If diameter consists of even number of nodes, then there are 2 centre nodes(also known as Bi-central Tree).
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Queries to find sum of distance of a given node to every leaf node in a Weighted Tree
- Find farthest node from each node in Tree
- Find the parent of a node in the given binary tree
- Find the Deepest Node in a Binary Tree
- Find GCD of each subtree of a given node in an N-ary Tree for Q queries
- Find the maximum Even Digit Sum node in the given tree
- Find root of the tree where children id sum for every node is given
- Find mirror of a given node in Binary tree
- Find the Kth node in the DFS traversal of a given subtree in a Tree
- Find n-th node in Preorder traversal of a Binary Tree
- Find the maximum node at a given level in a binary tree
- Find parent of each node in a tree for multiple queries
- Find n-th node in Postorder traversal of a Binary Tree
- Find the Deepest Node in a Binary Tree Using Queue STL - SET 2
- Find distance from root to given node in a binary tree
- Find the node with minimum value in a Binary Search Tree
- Find the node with maximum value in a Binary Search Tree
- Find the color of given node in an infinite binary tree
- Find parent of given node in a Binary Tree with given postorder traversal
- Find the kth node in vertical order traversal of a Binary Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.