Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Tuesday, July 25, 2017

Count Leaves in Binary Tree


Idea is to check if both left and right nodes are null if so increment value


Code:


int countLeaves(Node node) 
    {
        int count=0;
         if(node==null)
         return 0;
         if(node.left==null && node.right==null)
         return 1;
         else
     {
         count+=countLeaves(node.left);
         count+=countLeaves(node.right);
    }
    return count;
    }

0 Comments:

Post a Comment

Stats