• Courses
  • Tutorials
  • DSA
  • Data Science
  • Web Tech
August 22, 2024 |58.2K Views

Find the Maximum Depth or Height of a Tree

  Share   Like
Description
Discussion

Given a binary tree, find height of it. Height of empty tree is -1, height of tree with one node is 0 and height of below tree is 2. 
Algorithm:

maxDepth()
1. If tree is empty then return -1
2. Else
    (a) Get the max depth of left subtree recursively  i.e., 
         call maxDepth( tree->left-subtree)
    (a) Get the max depth of right subtree recursively  i.e., 
         call maxDepth( tree->right-subtree)
    (c) Get the max of max depths of left and right 
         subtrees and add 1 to it for the current node.
        max_depth = max(max dept of left subtree,  
                            max depth of right subtree) 
                            + 1
    (d) Return max_depth

Find the Maximum Depth or Height of a Tree: https://www.geeksforgeeks.org/write-a-c-program-to-find-the-maximum-depth-or-height-of-a-tree/