program to count leaf nodes in a binary tree in java

In this post, we will see about program to count leaf nodes in a binary tree in java

Algorithm-

Steps for counting number of leaf nodes are:
    • If node is null then return 0
    • If encountered leaf node(i.e. node.left is null and node.right is null) then return 1.
    • Recursively calculate number of leaf nodes using

Code for recursion will be:

Lets create java program for counting number of leaf nodes:

Run above program and you will get following output:

Please go through Frequently asked java interview programs  for more such programs.

Add Comment