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 */
10      l_depth = max_height (node->left); 
11      r_depth = max_height (node->right); 
12      /* use the longer one */
13      if (l_depth > r_depth) {
14        return (l_depth + 1);
15      } else {
16        return (r_depth + 1);
17      }
18    }
19  }

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

 Vote 0

Similar topics related to this section

Binary tree, traverse Binary tree, Binary search tree, double Binary tree, mirror Binary tree, height of Binary tree, heap, Complexity, Linear Search,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan