Google News
logo
Algorithm - Interview Questions
Write an algorithm for counting the number of leaf nodes in a binary tree.
An algorithm for counting the number of leaf nodes in a binary tree is given below :
 
Step 1 : If the current node is null, return a value 0.

Step 2 : If a leaf node is encountered, that is, if the current node's left and right nodes are both null, then return 1.

Step 3 : Calculate the number of leaf nodes recursively by adding the number of leaf nodes in the left subtree by the number of leaf nodes in the right subtree.
Advertisement