Description: The height of a tree is given by the number of nodes on the longest path from the root to the leaf. Given a tree, we have to find it’s height.

 
If video does not load, Click here
 
Example:

The tree given below has a height of 3.

 
Algorithm and implementation:

 

/*The following function is used to find height of the tree or portion of the tree recursively*/

int height(struct node* node)

{

  if (node==NULL)

      return 0;/*tree is empty*/

  else

  {

      int left_height = height(node->left);  /*height of left subtree*/

      int right_height = height(node->right);/*height of right subtree*/

 

      if (left_height > right_height)

          return(left_height+1);

      else return(right_height+1);

  }

}

Go to top