VOOZH about

URL: https://www.geeksforgeeks.org/dsa/maximum-height-of-tree-when-any-node-can-be-considered-as-root/

⇱ Maximum height of Tree when any Node can be considered as Root - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Maximum height of Tree when any Node can be considered as Root

Last Updated : 3 Dec, 2024

Given a tree with n nodes and n-1 edges, the task is to find the maximum height of the tree when any node in the tree is considered as the root of the tree. 

Example:

Input:

👁 Maximum-height-of-Tree-Example--3

Output: 5
Explanation: The below diagram represents a tree with 11 nodes and 10 edges and the path that gives us the maximum height when node 1 is considered as a root. The maximum height is 3.

👁 Maximum-height-of-Tree-Example-4


Input:

👁 Maximum-height-of-Tree-Example-1

Output: 4
Explanation: The below diagram represents a tree with 11 nodes and 10 edges and the path that gives us the maximum height when node 2 is considered as a root.

👁 Maximum-height-of-Tree-Example-2

Using Dynamic Programming - O(n) Time and O(n) Space

A naive approach would 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 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 a root, 

  • in[i] be the maximum height of the tree when we travel downwards via its sub-trees and leaves.
  • out[i] be the maximum height of the tree while traveling upwards via its parent. 

The maximum height of a tree when node i is considered as a root will be max(in[i], out[i]).

The below is the calculation of in[i]:

👁 Maximum-height-of-Tree-when-any-Node-can-be-considered-as-Root-1


In the image above, values 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. 

The below is the calculation of out[i]:

👁 Maximum-height-of-Tree-when-any-Node-can-be-considered-as-Root-2


The above diagram shows all the out[i] values (path for all the out[i] is shown below). 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. 

  • Node 1: No other way
  • Node 2: 2 -> 1 -> 3 -> 7 -> 10
  • Node 3 = 3 -> 1 -> 4 -> 8
  • Node 4 = 4 -> 1 -> 3 -> 7 -> 11
  • Node 5 = 5 -> 2 -> 1 -> 3 -> 7 -> 10
  • Node 6 = 6 -> 2 -> 1 -> 3 -> 7 -> 10
  • Node 7 = 7 -> 3 -> 1 -> 4 -> 9
  • Node 8 = 8 -> 4 -> 1 -> 3 -> 7 -> 11
  • Node 9 = 9 -> 4 -> 1 -> 3 -> 7 -> 11
  • Node 10 = 10-> 7 -> 3 -> 1 -> 2 -> 6
  • Node 11 = 11 -> 7 -> 3 -> 1 -> 4 -> 8
👁 Maximum-height-of-Tree-when-any-Node-can-be-considered-as-Root-3


The above diagram explains the calculation of out[i] when 2 is considered as the root of the tree. The branches of node 2 are not taken into count since the maximum height via that path has already been calculated and stored in in[2]. 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.

👁 Maximum-height-of-Tree-when-any-Node-can-be-considered-as-Root-4


The above diagram explains the calculation of out[10]. 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 the answer. Since the algorithm used is based on DFS, all the branches connected to the parent will be considered, including the branch which has the node. If the first maximum path thus obtained is the 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], longest path of all branches of parent of i) 

Output
5

Using Diameter of Tree - O(n) Time and O(h) Space

The idea of this approach is based on the property that the maximum height of a tree, when rooted at any node, is equal to the tree's diameter (the longest path between any two nodes).


Output
5
Comment
Article Tags:
Article Tags: