program to count leaf nodes in a binary tree in java

Last updated on June 4th, 2017 at 12:25 am

Previous
Next
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.

Previous
Next

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.

Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog