Binary Tree Max Depth/Height

Max height is computed by taking the longest height of any sub tree starting from root node. The logic is to iterate through either left sub tree or right subtree and consider the longer height and further iterate to that sub tree till we reach the leaf. This way we can obtain the maximum depth/height of a binary tree.

int max_height(struct node* node)
{
  int l_depth;
  int r_depth;

  if (node==NULL) { 
    return(0); 
  } else { 
    /* compute the height of each sub tree */
    l_depth = max_height (node->left); 
    r_depth = max_height (node->right); 
    /* use the longer one */
    if (l_depth > r_depth) {
      return (l_depth + 1);
    } else {
      return (r_depth + 1);
    }
  }
}

About our authors: Team EQA

You have viewed 1 page out of 252. Your C learning is 0.00% complete. Login to check your learning progress.

#