Given a tree with N nodes and N-1 edges, find out the maximum height of tree when any node in the tree is considered as the root of the tree.
The above diagram represents a tree with 11 nodes and 10 edges, and the path which gives us the maximum height when node 1 is considered as root. The maximum height is 3.
In the above diagram, when 2 is considered as root, then the longest path found is in RED color. A naive approach will be to traverse the tree using DFS traversal for every node and calculate the maximum height when the node is treated as the root of the tree. The time complexity for DFS traversal of a tree is O(N). The overall time complexity of DFS for all N nodes will be O(N)*N i.e., O(N2).
The above problem can be solved by using Dynamic Programming on Trees. To solve this problem, pre-calculate two things for every node. One will be the maximum height while traveling downwards via its branches to the leaves. While the other will be the maximum height when traveling upwards via its parent to any of the leaves.
Optimal Substructure :
When node i is considered as root,
in[i] be the maximum height of tree when we travel downwards via its sub-trees and leaves.
Also, out[i] be the maximum height of the tree while traveling upwards via its parent.
The maximum height of tree when node i is
considered as root will be max(in[i], out[i]).
Calculation of in[i] :
In the image above, values of in[i] have been calculated for every node i. The maximum of every subtree is taken and added with 1 to the parent of that subtree. Add 1 for the edge between parent and subtree. Traverse the tree using DFS and calculate in[i] as max(in[i], 1+in[child]) for every node.
Calculation of out[i] :
The above diagram shows all the out[i] values and the path. For calculation of out[i], move upwards to the parent of node i. From the parent of node i, there are two ways to move in, one will be in all the branches of the parent. The other direction is to move to the parent(call it parent2 to avoid confusion) of the parent(call it parent1) of node i. The maximum height upwards via parent2 is out[parent1] itself. Generally, out[node i] as 1+max(out[i], 1+max of all branches). Add 1 for the edges between node and parent.
The above diagram explains the calculation of out[i] when 2 is considered as the root of the tree. The branches of node 2 is not taken into count since the maximum height via that path has already been calculated and stored in i. Moving up, in this case, the parent of 2 i.e., 1 has no parent. So, the branches except for the one which has the node are considered while calculating the maximum.
The above diagram explains the calculation of out. The parent of node 10, i.e., 7 has a parent and a branch(precisely a child in this case). So the maximum height of both has been taken to count in such cases when parent and branches exist.
In case of multiple branches of a parent, take the longest of them to count(excluding the branch in which the node lies)
Calculating the maximum height of all the branches connected to parent :
in[i] stores the maximum height while moving downwards. No need to store all the lengths of branches. Only the first and second maximum length among all the branches will give answer. Since, algorithm used is based on DFS, all the branches connected to parent will be considered, including the branch which has the node. If the first maximum path thus obtained is same as in[i], then maximum1 is the length of the branch in which node i lies. In this case, our longest path will be maximum2.
Recurrence relation of in[i] and out[i] :
in[i] = max(in[i], 1 + in[child])
out[i] = 1 + max(out[parent of i], 1 + longest path of all branches of parent of i)
Below is the implementation of the above idea :
The maximum height when node 1 is considered as root is 3 The maximum height when node 2 is considered as root is 4 The maximum height when node 3 is considered as root is 3 The maximum height when node 4 is considered as root is 4 The maximum height when node 5 is considered as root is 5 The maximum height when node 6 is considered as root is 5 The maximum height when node 7 is considered as root is 4 The maximum height when node 8 is considered as root is 5 The maximum height when node 9 is considered as root is 5 The maximum height when node 10 is considered as root is 5 The maximum height when node 11 is considered as root is 5
Time Complexity : O(N)
Auxiliary Space : O(N)
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Dynamic Programming on Trees | Set-1
- Sum over Subsets | Dynamic Programming
- Bitmasking and Dynamic Programming | Set-2 (TSP)
- Dynamic Segment Trees : Online Queries for Range Sum with Point Updates
- Dynamic Programming | Building Bridges
- Greedy approach vs Dynamic programming
- Top 20 Dynamic Programming Interview Questions
- Ackermann's function using Dynamic programming
- How to solve a Dynamic Programming Problem ?
- Convert N to M with given operations using dynamic programming
- Number of Unique BST with a given key | Dynamic Programming
- Dynamic Programming vs Divide-and-Conquer
- Double Knapsack | Dynamic Programming
- Longest subsequence with a given OR value : Dynamic Programming Approach
- Overlapping Subproblems Property in Dynamic Programming | DP-1
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.