Height of Binary Tree | Practice | GeeksforGeeks

Given a binary tree, find its height.

Example 1:
Input:
     1
    /  \
   2    3
Output: 2

Example 2:
Input:
  2
   \
    1
   /
 3
Output: 3   

Your Task:
You don't need to read input or print anything. Your task is to complete the function height() which takes root node of the tree as input parameter and returns an integer denoting the height of the tree. If the tree is empty, return 0. 

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)

Constraints:
1 <= Number of nodes <= 105
1 <= Data of a node <= 105
-1
superrhitik4581 day ago
//                                              Jai Shri Ram..
//simple recursive solution
class Solution{   public:   //Function to find the height of a binary tree.   int height(struct Node* node){       // code here        if(node==NULL)       return 0;              return 1+max(height(node->left),height(node->right));   }}; 
0
alugojuvamshi1 day ago
          EASY C++ SOLUTION
 
int height(struct Node* node){   //code here   if(node==NULL)   return 0;      else    {       int l=height(node->left);       int r=height(node->right);               if(l>r)return (l+1);        else return (r+1);   }   } 
0
shubham211019971 day ago
 int height(Node node)    {       if(node==null)return 0;       if(node.left==null && node.right==null) return 1;//leaf node       int h1=height(node.left);       int h2=height(node.right);       return 1+Math.max(h1,h2);   }
0
virus1vinayak2 days ago
Java solution
0.29s 
 int height(Node node)    {       // code here        Stack<Node> r=new Stack<Node>();       r.push(node);       int h=0;       while(!r.isEmpty())       {           Node t=r.pop();           h+=1;           if(t.left!=null)            r.push(t.left);           else if(t.right!=null)           r.push(t.right);       }       return h;   }
0
bishal1289be214 days ago
class Solution{   public:   //Function to find the height of a binary tree.   int height(struct Node* node){       // code here         if(node==NULL){       return 0;   }      int left=height(node->left);   int right=height(node->right);   int ans=max(left,right)+1;   return ans;   }};
0
vishrut4445 days ago
JAVA (Recursion)
int height(Node node)    {       // code here        if(node==null) return 0;       else{           return Math.max(height(node.left),height(node.right))+1;       }   }
0
born2code1 week ago
int height(Node root) 
    {
        if(root==null)
            return 0;
        if(root.left==null&&root.right==null)
            return 1;
        else
            return Math.max(height(root.left), height(root.right))+1;
    }
0
ialtafshaikh1 week ago
#  Solution 2 := Using Level Order -----------------------def maxDepth(self, root: Optional[TreeNode]) -> int:       maxHeight = 0       # Base Case       if root is None:           return 0
       # Create an empty queue       # for level order traversal       queue = []
       # Enqueue Root and initialize height       queue.append(root)
       while(len(queue)):           for _ in range(len(queue)):               node = queue.pop(0)
               if node.left is not None:                   queue.append(node.left)
               if node.right is not None:                   queue.append(node.right)                      maxHeight+=1                  return maxHeight
0
shanu141 week ago
2 line solution [Recursion] : 
int height(struct Node* node){
        if(node==NULL) return 0; // Base Condition
        return 1+max(height(node->left),height(node->right));
    }
0
saurabhsrivastva1 week ago
Hii...Can someone explain why to take local variables l and r not as global variable , in this code.  int maxi_depth(TreeNode* root)   {          if(!root)return 0;          if(!root->left  && !root->right)return 1;                            int l=maxi_depth(root->left) ;               int r=maxi_depth(root->right);                   return 1+ max(l,r);                           } 
→Global Variable not gives appropriate result , here in this code. WHy?? 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.