Given a tree with N nodes and N-1 edges, find out the maximum height of the 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 that gives us the maximum height when node 1 is considered as a root. The maximum height is 3.

In the above diagram, when 2 is considered as a root, then the longest path found is in RED color. 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 time complexity for the 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 a root,
in[i] be the maximum height of the 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 a tree when node i is
considered as a root will be max(in[i], out[i]).
Calculation of in[i]:

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.
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 are not taken into count since the maximum height via that path has already been calculated and stored in i[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.

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], 1 + longest path of all branches of parent of i)
Below is the implementation of the above idea:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > in,out;
void dfs1(vector< int > v[], int u, int parent)
{
in[u] = 0;
for ( int child : v[u]) {
if (child == parent)
continue ;
dfs1(v, child, u);
in[u] = max(in[u], 1 + in[child]);
}
}
void dfs2(vector< int > v[], int u, int parent)
{
int mx1 = -1, mx2 = -1;
for ( int child : v[u]) {
if (child == parent)
continue ;
if (in[child] >= mx1) {
mx2 = mx1;
mx1 = in[child];
}
else if (in[child] > mx2)
mx2 = in[child];
}
for ( int child : v[u]) {
if (child == parent)
continue ;
int longest = mx1;
if (mx1 == in[child])
longest = mx2;
out[child] = 1 + max(out[u], 1 + longest);
dfs2(v, child, u);
}
}
void printHeights(vector< int > v[], int n)
{
dfs1(v, 1, 0);
dfs2(v, 1, 0);
for ( int i = 1; i <= n; i++)
cout << "The maximum height when node "
<< i << " is considered as root"
<< " is " << max(in[i], out[i])
<< "\n" ;
}
int main()
{
int n = 11;
vector< int > v[n + 1];
v[1].push_back(2), v[2].push_back(1);
v[1].push_back(3), v[3].push_back(1);
v[1].push_back(4), v[4].push_back(1);
v[2].push_back(5), v[5].push_back(2);
v[2].push_back(6), v[6].push_back(2);
v[3].push_back(7), v[7].push_back(3);
v[7].push_back(10), v[10].push_back(7);
v[7].push_back(11), v[11].push_back(7);
v[4].push_back(8), v[8].push_back(4);
v[4].push_back(9), v[9].push_back(4);
in.resize(n+1,0);
out.resize(n+1,0);
printHeights(v, n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static final int MAX_NODES = 100 ;
static int in[] = new int [MAX_NODES];
static int out[] = new int [MAX_NODES];
static void dfs1(ArrayList<ArrayList<Integer>> v,
int u, int parent)
{
in[u] = 0 ;
for ( int j = 0 ; j < v.get(u).size(); j++)
{
int child = v.get(u).get(j);
if (child == parent)
continue ;
dfs1(v, child, u);
in[u] = Math.max(in[u], 1 + in[child]);
}
}
static void dfs2(ArrayList<ArrayList<Integer>> v,
int u, int parent)
{
int mx1 = - 1 , mx2 = - 1 ;
for ( int j = 0 ; j < v.get(u).size(); j++)
{
int child = v.get(u).get(j);
if (child == parent)
continue ;
if (in[child] >= mx1)
{
mx2 = mx1;
mx1 = in[child];
}
else if (in[child] > mx2)
mx2 = in[child];
}
for ( int j = 0 ; j < v.get(u).size(); j++)
{
int child = v.get(u).get(j);
if (child == parent)
continue ;
int longest = mx1;
if (mx1 == in[child])
longest = mx2;
out[child] = 1 + Math.max(out[u], 1 + longest);
dfs2(v, child, u);
}
}
static void addEdge(ArrayList<ArrayList<Integer>> adj,
int u, int v)
{
adj.get(u).add(v);
adj.get(v).add(u);
}
static void printHeights(ArrayList<ArrayList<Integer>> v,
int n)
{
dfs1(v, 1 , 0 );
dfs2(v, 1 , 0 );
for ( int i = 1 ; i < n; i++)
System.out.println(
"The maximum height when node " + i +
" is considered as root is " +
Math.max(in[i], out[i]));
}
public static void main(String[] args)
{
int V = 12 ;
ArrayList<ArrayList<
Integer>> adj = new ArrayList<ArrayList<
Integer>>(V + 1 );
for ( int i = 0 ; i < V; i++)
adj.add( new ArrayList<Integer>());
addEdge(adj, 1 , 2 );
addEdge(adj, 1 , 3 );
addEdge(adj, 1 , 4 );
addEdge(adj, 2 , 5 );
addEdge(adj, 2 , 6 );
addEdge(adj, 3 , 7 );
addEdge(adj, 7 , 10 );
addEdge(adj, 7 , 11 );
addEdge(adj, 4 , 8 );
addEdge(adj, 4 , 9 );
printHeights(adj, V);
}
}
|
Python3
inn = [ 0 ] * 100
out = [ 0 ] * 100
def dfs1(v, u, parent):
global inn, out
inn[u] = 0
for child in v[u]:
if (child = = parent):
continue
dfs1(v, child, u)
inn[u] = max (inn[u], 1 + inn[child])
def dfs2(v, u, parent):
global inn, out
mx1, mx2 = - 1 , - 1
for child in v[u]:
if (child = = parent):
continue
if (inn[child] > = mx1):
mx2 = mx1
mx1 = inn[child]
elif (inn[child] > mx2):
mx2 = inn[child]
for child in v[u]:
if (child = = parent):
continue
longest = mx1
if (mx1 = = inn[child]):
longest = mx2
out[child] = 1 + max (out[u], 1 + longest)
dfs2(v, child, u)
def printHeights(v, n):
global inn, out
dfs1(v, 1 , 0 )
dfs2(v, 1 , 0 )
for i in range ( 1 , n + 1 ):
print ( "The maximum height when node" , i, "is considered as root is" , max (inn[i], out[i]))
if __name__ = = '__main__' :
n = 11
v = [[] for i in range (n + 1 )]
v[ 1 ].append( 2 )
v[ 2 ].append( 1 )
v[ 1 ].append( 3 )
v[ 3 ].append( 1 )
v[ 1 ].append( 4 )
v[ 4 ].append( 1 )
v[ 2 ].append( 5 )
v[ 5 ].append( 2 )
v[ 2 ].append( 6 )
v[ 6 ].append( 2 )
v[ 3 ].append( 7 )
v[ 7 ].append( 3 )
v[ 7 ].append( 10 )
v[ 10 ].append( 7 )
v[ 7 ].append( 11 )
v[ 11 ].append( 7 )
v[ 4 ].append( 8 )
v[ 8 ].append( 4 )
v[ 4 ].append( 9 )
v[ 9 ].append( 4 )
printHeights(v, n)
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static int MAX_NODES = 100;
static int [] In = new int [MAX_NODES];
static int [] Out = new int [MAX_NODES];
static void dfs1(List<List< int >> v,
int u, int parent)
{
In[u] = 0;
for ( int j = 0; j < v[u].Count; j++)
{
int child = v[u][j];
if (child == parent)
continue ;
dfs1(v, child, u);
In[u] = Math.Max(In[u], 1 + In[child]);
}
}
static void dfs2(List<List< int >> v,
int u, int parent)
{
int mx1 = -1, mx2 = -1;
for ( int j = 0; j < v[u].Count; j++)
{
int child = v[u][j];
if (child == parent)
continue ;
if (In[child] >= mx1)
{
mx2 = mx1;
mx1 = In[child];
}
else if (In[child] > mx2)
mx2 = In[child];
}
for ( int j = 0; j < v[u].Count; j++)
{
int child = v[u][j];
if (child == parent)
continue ;
int longest = mx1;
if (mx1 == In[child])
longest = mx2;
Out[child] = 1 + Math.Max(Out[u], 1 + longest);
dfs2(v, child, u);
}
}
static void addEdge(List<List< int >> adj,
int u, int v)
{
adj[u].Add(v);
adj[v].Add(u);
}
static void printHeights(List<List< int >> v,
int n)
{
dfs1(v, 1, 0);
dfs2(v, 1, 0);
for ( int i = 1; i < n; i++)
Console.WriteLine(
"The maximum height when node " + i +
" is considered as root is " +
Math.Max(In[i], Out[i]));
}
static public void Main (){
int V = 12;
List<List< int >> adj = new List<List<
int >>();
for ( int i = 0; i < V; i++)
adj.Add( new List< int >());
addEdge(adj, 1, 2);
addEdge(adj, 1, 3);
addEdge(adj, 1, 4);
addEdge(adj, 2, 5);
addEdge(adj, 2, 6);
addEdge(adj, 3, 7);
addEdge(adj, 7, 10);
addEdge(adj, 7, 11);
addEdge(adj, 4, 8);
addEdge(adj, 4, 9);
printHeights(adj, V);
}
}
|
Javascript
<script>
let MAX_NODES = 100;
let In = new Array(MAX_NODES);
let out= new Array(MAX_NODES);
for (let i=0;i<MAX_NODES;i++)
{
In[i]=0;
out[i]=0;
}
function dfs1(v,u,parent)
{
In[u] = 0;
for (let j = 0; j < v[u].length; j++)
{
let child = v[u][j];
if (child == parent)
continue ;
dfs1(v, child, u);
In[u] = Math.max(In[u], 1 + In[child]);
}
}
function dfs2(v,u,parent)
{
let mx1 = -1, mx2 = -1;
for (let j = 0; j < v[u].length; j++)
{
let child = v[u][j];
if (child == parent)
continue ;
if (In[child] >= mx1)
{
mx2 = mx1;
mx1 = In[child];
}
else if (In[child] > mx2)
mx2 = In[child];
}
for (let j = 0; j < v[u].length; j++)
{
let child = v[u][j];
if (child == parent)
continue ;
let longest = mx1;
if (mx1 == In[child])
longest = mx2;
out[child] = 1 + Math.max(out[u], 1 + longest);
dfs2(v, child, u);
}
}
function addEdge(adj,u,v)
{
adj[u].push(v);
adj[v].push(u);
}
function printHeights(v,n)
{
dfs1(v, 1, 0);
dfs2(v, 1, 0);
for (let i = 1; i < n; i++)
document.write(
"The maximum height when node " + i +
" is considered as root is " +
Math.max(In[i], out[i])+ "<br>" );
}
let V = 12;
let adj = new Array(V+1);
for (let i = 0; i <= V; i++)
adj[i]=[];
addEdge(adj, 1, 2);
addEdge(adj, 1, 3);
addEdge(adj, 1, 4);
addEdge(adj, 2, 5);
addEdge(adj, 2, 6);
addEdge(adj, 3, 7);
addEdge(adj, 7, 10);
addEdge(adj, 7, 11);
addEdge(adj, 4, 8);
addEdge(adj, 4, 9);
printHeights(adj, V);
</script>
|
Output :
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)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
22 Dec, 2022
Like Article
Save Article