find tree height code example

Example 1: find height of binary search tree python

def getHeight(self,root):
        return -1 if root is None else 1 + max(self.getHeight(root.left), self.getHeight(root.right))

Example 2: find height of a tree

// finding height of a binary tree in c++.
int maxDepth(node* node)  
{  
    if (node == NULL)  
        return 0;  
    else
    {  
        /* compute the depth of each subtree */
        int lDepth = maxDepth(node->left);  
        int rDepth = maxDepth(node->right);  
      
        /* use the larger one */
        if (lDepth > rDepth)  
            return(lDepth + 1);  
        else return(rDepth + 1);  
    }  
}

Tags:

Cpp Example