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)
- Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree
- Sum of all odd nodes in the path connecting two given nodes
- Find maximum among all right nodes in Binary Tree
- Level with maximum number of nodes
- Level with maximum number of nodes using DFS in a N-ary tree
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Maximum number of leaf nodes that can be visited within the given budget
- Implementing a BST where every node stores the maximum number of nodes in the path till any leaf
- Print levels with odd number of nodes and even number of nodes
- Find Maximum number possible by doing at-most K swaps
- Number of triplets in array having subarray xor equal
- Sum of the distances from every node to all other nodes is maximum
- Sum of nodes at maximum depth of a Binary Tree
- Maximum sum of nodes in Binary tree such that no two are adjacent
- Sum of nodes at maximum depth of a Binary Tree | Set 2
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.
Improved By : rituraj_jain