(Translated by https://www.hiragana.jp/)
Maximum height of Tree when any Node can be considered as Root - GeeksforGeeks
Open In App

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

Last Updated : 03 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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) 
C++
// C++ code to find the maximum path length
// considering any node as root
#include <bits/stdc++.h>
using namespace std;

class Node{
public:
    int data;
    vector<Node*> children;
    Node(int x) {
        data = x;
    }
};

// function to pre-calculate the in[]
// which stores the maximum height when travelled
// via branches
int dfs1(Node* root, unordered_map<Node*,int> &in) {
    
    int ans = 0;
    
    for (Node* child: root->children) {
        ans = max(ans, 1+dfs1(child, in));
    }
    
    return in[root] = ans;
}

// function to pre-calculate the array out[]
// which stores the maximum height when traveled
// via parent
void dfs2(Node* root, unordered_map<Node*,int> &out, 
unordered_map<Node*,int> &in) {
    
    // stores the longest and second 
    // longest branches
    int mx1 = -1, mx2 = -1;

    // traverse in the subtrees of root
    for (Node* child : root->children) {

        // compare and store the longest
        // and second longest
        if (in[child] >= mx1) {
            mx2 = mx1;
            mx1 = in[child];
        }

        else if (in[child] > mx2)
            mx2 = in[child];
    }

    // traverse in the subtree of root
    for (Node* child : root->children) {
        int longest = mx1;

        // if longest branch has the node, then
        // consider the second longest branch
        if (mx1 == in[child])
            longest = mx2;

        // recursively calculate out[i]
        out[child] = 1 + max(out[root], 1 + longest);

        // dfs function call
        dfs2(child, out,in);
    }
}

// function to get maximum height.
int maxHeight(Node* root) {
    unordered_map<Node*,int> in, out;
    
    // traversal to calculate in values
    dfs1(root, in);

    // traversal to calculate out values
    dfs2(root, out, in);
    
    int ans = 0;

    for (auto p: in) {
        Node* node = p.first;
        ans = max({ans, in[node], out[node]});
    }
    
    return ans;
}

int main() {
  
    Node* root = new Node(1);
    Node* node1 = new Node(2);
    Node* node2 = new Node(3);
    Node* node3 = new Node(4);
    Node* node4 = new Node(5);
    Node* node5 = new Node(6);
    Node* node6 = new Node(7);
    Node* node7 = new Node(8);
    Node* node8 = new Node(9);
    Node* node9 = new Node(10);
    Node* node10 = new Node(11);

    root->children.push_back(node1);
    root->children.push_back(node2);
    root->children.push_back(node3);
    node1->children.push_back(node4);
    node1->children.push_back(node5);
    node2->children.push_back(node6);
    node3->children.push_back(node7);
    node3->children.push_back(node8);
    node6->children.push_back(node9);
    node6->children.push_back(node10);
    
    cout << maxHeight(root);

    return 0;
}
Java Python C# JavaScript

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).

C++
// C++ code to find the maximum path length
// considering any node as root
#include <bits/stdc++.h>
using namespace std;

class Node{
public:
    int data;
    vector<Node*> children;
    Node(int x) {
        data = x;
    }
};

// Function to get diameter of tree.
int diameter(Node* root, int &ans) {
    
    // variables to get the 2 longest
    // paths from current node.
    int mx1 = 0, mx2 = 0;
    
    for (Node* child: root->children) {
        int val = 1+diameter(child, ans);
        
        if (val>mx1) {
            mx2 = mx1;
            mx1 = val;
        }
        else if(val>mx2) {
            mx2 = val;
        }
    }
    
    ans = max(ans, mx1+mx2);
    
    return mx1;
}

// function to get maximum height.
int maxHeight(Node* root) {
    int ans = 0;
    diameter(root, ans);
    return ans;
}

int main() {
    Node* root = new Node(1);
    Node* node1 = new Node(2);
    Node* node2 = new Node(3);
    Node* node3 = new Node(4);
    Node* node4 = new Node(5);
    Node* node5 = new Node(6);
    Node* node6 = new Node(7);
    Node* node7 = new Node(8);
    Node* node8 = new Node(9);
    Node* node9 = new Node(10);
    Node* node10 = new Node(11);

    root->children.push_back(node1);
    root->children.push_back(node2);
    root->children.push_back(node3);
    node1->children.push_back(node4);
    node1->children.push_back(node5);
    node2->children.push_back(node6);
    node3->children.push_back(node7);
    node3->children.push_back(node8);
    node6->children.push_back(node9);
    node6->children.push_back(node10);
    
    cout << maxHeight(root);

    return 0;
}
Java Python C# JavaScript

Output
5

Article Tags :

Similar Reads