Given a Tree, the task is to find the farthest node from each node to another node in this tree.
Input: Given Adjacency List of Below Tree:
Farthest node from node 1: 6
Farthest node from node 2: 6
Farthest node from node 3: 6
Farthest node from node 4: 6
Farthest node from node 5: 1
Farthest node from node 6: 1
Farthest node from node 1: 4
Farthest node from node 2: 7
Farthest node from node 3: 4
Farthest node from node 4: 7
Farthest node from node 5: 7
Farthest node from node 6: 4
Farthest node from node 7: 4
First, we have to find two end vertices of the diameter and to find that, we will choose an arbitrary vertex and find the farthest node from this arbitrary vertex and this node will be one end of the diameter and then make it root to find farthest node from it, which will be the other end of diameter. Now for each node, the farthest node will be one of these two end vertices of the diameter of the tree.
Why it works?
Let x and y are the two end vertices of the diameter of the tree and a random vertex is u. Let the farthest vertex from u is v, not x or y. As v is the farthest from u then a new diameter will form having end vertices as x, v or y, v which has greater length but a tree has a unique length of the diameter, so it is not possible and the farthest vertex from u must be x or y.
Below is the implementation of above approach:
4 4 2 2 2
Time Complexity: O(V+E)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Minimum value of distance of farthest node in a Graph
- Find the node at the centre of an N-ary tree
- Find mirror of a given node in Binary tree
- Find the parent of a node in the given binary tree
- Find the Kth node in the DFS traversal of a given subtree in a Tree
- Find the maximum Even Digit Sum node in the given tree
- Find GCD of each subtree of a given node in an N-ary Tree for Q queries
- Find the Deepest Node in a Binary Tree
- Find root of the tree where children id sum for every node is given
- Find n-th node in Preorder traversal of a Binary Tree
- Find the color of given node in an infinite binary tree
- Find the maximum node at a given level in a binary tree
- Find distance from root to given node in a binary tree
- Find the node with maximum value in a Binary Search Tree
- Find parent of each node in a tree for multiple queries
- Find the Deepest Node in a Binary Tree Using Queue STL - SET 2
- Find n-th node in Postorder traversal of a Binary Tree
- Find the node with minimum value in a Binary Search Tree
- Find the kth node in vertical order traversal of a Binary Tree
- Find the node with maximum value in a Binary Search Tree using recursion
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.