Given a Tree with N nodes, the task is to find a triplet of nodes (a, b, c) such that the number of nodes covered in the path connecting these nodes is maximum. (Count a node only once).
Input: N = 4
Output: (2, 3, 4)
(2, 3, 4) as path between (2, 3) covers nodes 2, 1, 3 and path between (3, 4) covers nodes 3, 1, 4. Hence all nodes are covered.
The Red Path in Tree denotes the path between 2 to 3 node which covers node 1, 2, 3. The green path denotes path between (3, 4) which covers node 3, 1, 4.
Input: N = 9
Edge Set :
Output: (6, 8, 1)
- One important point to notice is, two of the points in triplet must be the end of diameter of tree to cover maximum of the points.
- We need to find the longest length branch stick to the diameter.
- Now for 3rd node, apply DFS along with maintaining the depth for each node (DFS in all directions other than on the Diameter Path Selected ) to all the nodes present on the path of Diameter, the node which is at the farthest distance, would be considered as the 3rd node, as it covers the maximum node other than already covered by the Diameter. Diameter of Tree using DFS
Below is the implementation of the above approach:
(2, 3, 4)
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.
- Sum of all odd nodes in the path connecting two given nodes
- Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree
- Largest component size in a graph formed by connecting non-co-prime nodes
- Diameters for each node of Tree after connecting it with given disconnected component
- Minimum number of Nodes to be removed such that no subtree has more than K nodes
- Print levels with odd number of nodes and even number of nodes
- Maximum sum of nodes in Binary tree such that no two are adjacent
- Maximum sum of nodes in Binary tree such that no two are adjacent | Dynamic Programming
- Triplet with a given sum in BST | Set 2
- Check if every vertex triplet in graph contains two vertices connected to third vertex
- Number of triplets in array having subarray xor equal
- Minimum number of groups of nodes such that no ancestor is present in the same group
- Number of distinct pair of edges such that it partitions both trees into same subsets of nodes
- Find a number such that maximum in array is minimum possible after XOR
- Find M such that GCD of M and given number N is maximum
- Common nodes in the inorder sequence of a tree between given two nodes in O(1) space
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Count of all prime weight nodes between given nodes in the given Tree
- Find maximum among all right nodes in Binary Tree
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect 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.