how to print leaf nodes of a binary tree in java

If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions.

This is 7th part of java binary tree tutorial.

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


Steps for counting number of leaf nodes are:
  • If node is null then return 0
  • If encounterd leaf node(i.e. node.left is null and node.right is null) then print the node.
  • Recursively visit leaf subtree and right subtree.

Code for recursion will be:

Binary tree:

Lets create java program for counting number of leaf nodes:

Run above program and you will get following output:

Java Binary tree tutorial:

Was this post helpful?


You may also like:

Related Posts

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.