Home » Data Structure & Algorithms » Count of Leaf Nodes in a Binary Tree

Count of Leaf Nodes in a Binary Tree

 

Question :
WAP to program to count the number of leaf nodes in a binary tree in O(n) time complexity.

 

 

Solution :
The problem is solved in recussive way.

Algorithm :
A leaf node is a node which has no left and right child.
Step 1 : If node is NULL then return 0.
Step 2 : If node has no left and right child then return 1.
Step 3 : Recurssively traverse left sub-tree and right sub-tree to return count of leaf nodes.

Program :

 


Leave a comment

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

fifteen + 17 =