Given a parent array P, where P[i] indicates the parent of the ith node in the tree(assume parent of root node id indicated with -1). Find the height of the tree.
Examples:
Input : array[] = [-1 0 1 6 6 0 0 2 7]
Output : height = 5
Tree formed is:
0
/ | \
5 1 6
/ | \
2 4 3
/
7
/
8
- Start at each node and keep going to its parent until we reach -1.
- Also, keep track of the maximum height between all nodes.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int findHeight( int * parent, int n)
{
int res = 0;
for ( int i = 0; i < n; i++) {
int p = i, current = 1;
while (parent[p] != -1) {
current++;
p = parent[p];
}
res = max(res, current);
}
return res;
}
int main()
{
int parent[] = { -1, 0, 1, 6, 6, 0, 0, 2, 7 };
int n = sizeof (parent) / sizeof (parent[0]);
int height = findHeight(parent, n);
cout << "Height of the given tree is: "
<< height << endl;
return 0;
}
|
Java
import java.io.*;
public class GFG {
static int findHeight( int [] parent, int n)
{
int res = 0 ;
for ( int i = 0 ; i < n; i++) {
int p = i, current = 1 ;
while (parent[p] != - 1 ) {
current++;
p = parent[p];
}
res = Math.max(res, current);
}
return res;
}
static public void main(String[] args)
{
int [] parent = { - 1 , 0 , 1 , 6 , 6 , 0 ,
0 , 2 , 7 };
int n = parent.length;
int height = findHeight(parent, n);
System.out.println( "Height of the "
+ "given tree is: " + height);
}
}
|
Python3
def findHeight(parent, n):
res = 0
for i in range (n):
p = i
current = 1
while (parent[p] ! = - 1 ):
current + = 1
p = parent[p]
res = max (res, current)
return res
if __name__ = = '__main__' :
parent = [ - 1 , 0 , 1 , 6 , 6 , 0 , 0 , 2 , 7 ]
n = len (parent)
height = findHeight(parent, n)
print ( "Height of the given tree is:" , height)
|
C#
using System;
public class GFG {
static int findHeight( int [] parent, int n)
{
int res = 0;
for ( int i = 0; i < n; i++) {
int p = i, current = 1;
while (parent[p] != -1) {
current++;
p = parent[p];
}
res = Math.Max(res, current);
}
return res;
}
static public void Main()
{
int [] parent = { -1, 0, 1, 6, 6, 0,
0, 2, 7 };
int n = parent.Length;
int height = findHeight(parent, n);
Console.WriteLine( "Height of the "
+ "given tree is: " + height);
}
}
|
Javascript
<script>
function findHeight(parent,n)
{
let res = 0;
for (let i = 0; i < n; i++) {
let p = i, current = 1;
while (parent[p] != -1) {
current++;
p = parent[p];
}
res = Math.max(res, current);
}
return res;
}
let parent=[-1, 0, 1, 6, 6, 0,
0, 2, 7];
let n = parent.length;
let height = findHeight(parent, n);
document.write( "Height of the "
+ "given tree is: " + height);
</script>
|
Output:
Height of the given tree is: 5
Time Complexity : O( N^2 )
Space Complexity : O( 1 )
Optimized approach: We use dynamic programming. We store the height from root to each node in an array. So, if we know the height of the root to a node, then we can get the height from the root to the node child by simply adding 1.
Implementation:
CPP
#include <bits/stdc++.h>
using namespace std;
int rec( int i, int parent[], vector< int > height)
{
if (parent[i] == -1) {
return 1;
}
if (height[i] != -1) {
return height[i];
}
height[i] = rec(parent[i], parent, height) + 1;
return height[i];
}
int findHeight( int * parent, int n)
{
int res = 0;
vector< int > height(n, -1);
for ( int i = 0; i < n; i++) {
res = max(res, rec(i, parent, height));
}
return res;
}
int main()
{
int parent[] = { -1, 0, 1, 6, 6, 0, 0, 2, 7 };
int n = sizeof (parent) / sizeof (parent[0]);
int height = findHeight(parent, n);
cout << "Height of the given tree is: "
<< height << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int rec( int i, int parent[], int [] height)
{
if (parent[i] == - 1 ) {
return 1 ;
}
if (height[i] != - 1 ) {
return height[i];
}
height[i] = rec(parent[i], parent, height) + 1 ;
return height[i];
}
static int findHeight( int [] parent, int n)
{
int res = 0 ;
int height[]= new int [n];
Arrays.fill(height,- 1 );
for ( int i = 0 ; i < n; i++) {
res = Math.max(res, rec(i, parent, height));
}
return res;
}
public static void main (String[] args) {
int [] parent = { - 1 , 0 , 1 , 6 , 6 , 0 , 0 , 2 , 7 };
int n = parent.length;
int height = findHeight(parent, n);
System.out.println( "Height of the given tree is: " +height);
}
}
|
Python3
def rec(i, parent, height):
if (parent[i] = = - 1 ):
return 1
if (height[i] ! = - 1 ):
return height[i]
height[i] = rec(parent[i], parent, height) + 1
return height[i]
def findHeight(parent, n):
res = 0
height = [ - 1 ] * (n)
for i in range (n):
res = max (res, rec(i, parent, height))
return res
if __name__ = = '__main__' :
parent = [ - 1 , 0 , 1 , 6 , 6 , 0 , 0 , 2 , 7 ]
n = len (parent)
height = findHeight(parent, n)
print ( "Height of the given tree is: " ,height)
|
C#
using System;
public class GFG{
static int rec( int i, int [] parent, int [] height)
{
if (parent[i] == -1) {
return 1;
}
if (height[i] != -1) {
return height[i];
}
height[i] = rec(parent[i], parent, height) + 1;
return height[i];
}
static int findHeight( int [] parent, int n)
{
int res = 0;
int [] height = new int [n];
Array.Fill(height, -1);
for ( int i = 0; i < n; i++) {
res = Math.Max(res, rec(i, parent, height));
}
return res;
}
static public void Main ()
{
int [] parent = { -1, 0, 1, 6, 6, 0, 0, 2, 7 };
int n = parent.Length;
int height = findHeight(parent, n);
Console.WriteLine( "Height of the given tree is: " +height);
}
}
|
Javascript
<script>
function rec(i,parent,height)
{
if (parent[i] == -1) {
return 1;
}
if (height[i] != -1) {
return height[i];
}
height[i] = rec(parent[i], parent, height) + 1;
return height[i];
}
function findHeight(parent,n)
{
let res = 0;
let height= new Array(n);
for (let i=0;i<n;i++)
{
height[i]=-1;
}
for (let i = 0; i < n; i++) {
res = Math.max(res, rec(i, parent, height));
}
return res;
}
let parent=[-1, 0, 1, 6, 6, 0, 0, 2, 7];
let n=parent.length;
let height = findHeight(parent, n);
document.write( "Height of the given tree is: " +height+ "<br>" );
</script>
|
Output:
Height of the given tree is: 5
Time complexity :- O(n)
Space complexity :- O(n)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
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 :
08 Mar, 2023
Like Article
Save Article