Find the Maximum Depth OR Height of a Binary Tree

Objective: Given a binary tree, find the height of it

Input: A Binary Tree

Output: Height of a binary tree


Tree Height - Example



  • Get the height of left sub tree, say leftHeight
  • Get the height of right sub tree, say rightHeight
  • Take the Max(leftHeight, rightHeight) and add 1 for the root and return
  • Call recursively.

Time Complexity : O(n)

get Height of a tree - Recursion

Complete Code:


Height of the Tree is 7

Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.

You may also like...

4 Responses

  1. Anuj says:

    Explanation with diagram is just amazing! Thanks!

  2. Ardalan Yousefi says:

    The height of a single-node tree is zero by definition, not one. You should check if your root has any children before adding 1 to the height.

    • Rui Tang (Nathan) says:

      public int treeHeight(Node root){
      if(root==null)return -1;
      return (1+ Math.max(treeHeight(root.left),treeHeight(root.right)));

      This would work

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: