Count Leaves in Binary Tree | Practice | GeeksforGeeks

Given a Binary Tree of size N , You have to count leaves in it. For example, there are two leaves in following tree
        1
     /      \
   10      39
  /
5

 
Example 1:

Input:
Given Tree is 
               4
             /   \
            8     10
           /     /   \
          7     5     1
         /
        3 
Output:
3
Explanation: 
Three leaves are 3 , 5 and 1.
 
Your Task:
You don't have to take input. Complete the function countLeaves() that takes root node of given tree as parameter and returns the count of leaves in tree . The printing is done by the driver code.
 
Constraints:
1<= N <= 104
 
Note:The Input/Ouput format and Example given below is used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console, and should not print anything on stdout/console. The task is to complete the function specified, and not to write the full code.
0
kashifahmad5993 days ago
When I submit the code, it's showing wrong answer for test case 3 4 2 but when I run this test case in custom input the output I get is correct.
 
 int c=0;int count(Node* root){    if(root==NULL) return 0;        int lh = count(root->left);    int rh =  count(root->right);        if(lh==0 && rh==0)    {        c++;    }        return root->data;}int countLeaves(Node* root){   count(root);   return c;}
0
yaswa7792 weeks ago
Why some test cases are not running
int x=0,y=0;int countLeaves(Node* root){   if(root){       x = countLeaves(root->left);       y = countLeaves(root->right);       return x+y+1;   }   return 0; }
0
harshscode2 weeks ago
int cnt=0;   if(!root)   return cnt;   cnt=(!root->left && !root->right)?(cnt=cnt+1):(countLeaves(root->left)+countLeaves(root->right));   return cnt;
0
bheniavedant2 weeks ago
Can anyone show me, a non recursive approach for this question in python? 
0
shailendramaheshwari172 weeks ago
C++
int countLeaves(Node* root)
{
 // Your code here
 if(root == NULL) return 0;
 int left = countLeaves(root->left);
 int right = countLeaves(root->right);
 if(left == 0 && right == 0) return (left+right+1);
 return (left+right);
}
0
anshsangwan1232 weeks ago
if(root->left ==NULL && root->right==NULL){ count++;}
0
anshulgupta966263 weeks ago
queue<Node*> q;
    int count = 0;
    q.push(root);
    while(!q.empty()){
        Node *front = q.front();
        q.pop();
        
        if(front->left == NULL && front->right == NULL){
            count++;
        }
        
        if(front->left != NULL){
            q.push(front->left);
        }
        if(front->right != NULL){
            q.push(front->right);
        }
    }
    return count; 
0
dipanshusharma93133 weeks ago
// java solution
class Tree{   static int x = 0;   int countLeaves(Node node)    {        // Your code         if(node == null){            return 0;        }        int x = 0;        if(node.left == null && node.right == null){            x++;        }        x = x + countLeaves(node.left);        x = x + countLeaves(node.right);        return x;   }}
0
shubhamtripathy3 weeks ago
Easy Python Approach
 
def countLeaves(root):
   # Code here
   if root is None:
       return 0
   if root.left is None and root.right is None:
       return 1
   
   return countLeaves(root.left)+countLeaves(root.right)
0
00thirt13n3 weeks ago
C++ Solution:
int countLeaves(Node* root){
  if(!root) return 0;
  if(!root->left && !root->right) return 1;
  return countLeaves(root->left)+countLeaves(root->right);
}
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.