Print All Leaf Nodes of a Binary Tree

Print All Leaf Nodes of a Binary Tree” is a basic problem of tree data structure. Here, we are given a binary tree and our task is to print all leaf nodes of given binary tree from left to right.

Leaf Nodes: Leaf Nodes are those nodes/vertices of a tree, which have no children.

A root node is considered to be a leaf node if there are no children of it.

Print All Leaf Nodes of a Binary Tree
Leaf Nodes of Binary Tree

Here, to print leaf nodes of a binary tree, we will modify the existing recursive preorder traversal algorithm. 

The steps required to print all leaf Nodes of a Binary Tree are as follows:

  1. If root is NULL, then return.
  2. If root is leaf node, print the data.
  3. If left or right child exists of root node, then call function for left and right child of the node recursively.

C++ Program to print all leaf nodes of a binary tree is as follows:

/* C++ Program to Print all leaf nodes of a Binary Tree */
#include<bits/stdc++.h>
using namespace std;

typedef struct Node
{
    int data;
    struct Node *left;
    struct Node *right;
    Node(int ele)
    {
        data = ele;
        left = NULL;
        right = NULL;
    }
} Node;

/* Function to Print all leaf nodes of a binary tree */
void printLeafNodes(Node *root)
{
    /* If root is empty, return */
    if(root == NULL)
    return;
    
    /* If root is leaf node, print data */
    if(root -> left == NULL && root -> right == NULL)
    cout<<root -> data << " ";
    
    /* If there exists left child, recursively call left subtree */
    if(root -> left)
    printLeafNodes(root -> left);
    
    /* If there exists right child, recursively call right subtree */
    if(root -> right)
    printLeafNodes(root -> right);
}

int main()
{
    /* Creating a Binary tree and inserting some nodes in it */
    Node *root = NULL;
    
    root = new Node(1);
    root -> left = new Node(2);
    root -> right = new Node(3);
    root -> left -> left = new Node(4);
    root -> left -> right = new Node(5);
    root -> right -> left = new Node(6);
    root -> right -> right = new Node (7);
    root -> left -> right -> left = new Node(8);
    root -> right -> left -> right = new Node(9);
    
    /* Calling function to print all leaf nodes of a Binary Tree */
    cout<<"The Leaf Nodes of the Tree are ";
    printLeafNodes(root);
}

OUTPUT:

The Leaf Nodes of the Tree are 4 8 9 7

Related Posts:

  1. Binary Tree Traversals
  2. Count Number of Nodes in a Binary Tree
  3. Print Alternate Levels of Binary Tree
  4. Maximum Width of Binary Tree
  5. Level Order Tree Traversal
  6. Left View of Binary Tree
  7. Right View of Binary Tree
  8. Compute Height of Binary Tree
  9. Inorder Tree Traversal Using Stack
  10. Preorder Tree Trasversal Using Stack
  11. Postorder Tree Traversal Using Stack
  12. Vertical Order Tree Traversal
  13. Top View of Binary Tree
  14. Bottom View of Binary Tree
  15. Delete Complete Binary Tree
  16. Check if two trees are mirror Trees of Each Other
  17. Infix to Postfix Conversion
  18. Convert Binary Tree to its Mirror Tree
  19. Check if Binary Tree is Symmetric or Not
  20. Print All Root to Leaf Paths in a Binary Tree

You may also like...