Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Print All Leaf Node of Binary Tree

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.23k
    Comment on it

    Hello Everyone!!!
     

    In order to solve binary tree problem of leaf node, we should know that what is leaf node? Leaf node is node of the binary tree whose have no left and right child means whose left and right child is null. It is the last node of the binary tree. We can print the all leaf node of the binary tree using Recursion but we can print it without recursion.

    Using Recursion:-

    It is the easy way of printing the binary tree leaf node because we are apply the same algorithm for the left node and also for the right node and at last we combine the results.

    Algorithm:-

    1.If given node is null then return.

    2.If left and right node are null, print .

    3. Recursively visit left and right subtree.

    public static void printLeafNodesOfBT(TreeNode node) {
        if (node == null) {
          return;
        }
    
        if (node.left == null && node.right == null) {
          System.out.printf("%d ", node.value);
        }
    
        printLeafNodesOfBT(node.left);
        printLeafNodesOfBT(node.right);
      }
    
    

    We uses this block of code for printing the leaf node of the binary tree using recursion.

    Using Iteration:-  

    It is another method for printing the leaf node of the binary tree using iteration. It is dificult to the recursion method.

    Algorithm:- 

    1. Create the stack.

    2. Push the root node.

    3.Loop until stack is not empty.

    4.Call stack.pop to get the last element and store its left and last child if they are not null.

    5.If the left and last child is null, print the value.

    public static void printLeafNodesOfBT(TreeNode root) {
    
        if (root == null) {
          return;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
    
        while (!stack.isEmpty()) {
          TreeNode node = stack.pop();
    
          if (node.right != null) {
            stack.add(node.right);
          }
    
          if (node.left != null) {
            stack.add(node.left);
          }
    
          if (node.left == null && node.right == null) {
            System.out.printf("%d ", node.value);
          }
    
        }
    
      }
    
    

    Here is the program for printing all leaf node.

    Program:-

    
    import java.util.Stack;
    public class Main {
    
      public static void main(String[] args) throws Exception {
    
        // let's create a binary tree
        TreeNode n10 = new TreeNode(10);
        TreeNode n34 = new TreeNode(34);
        TreeNode n43 = new TreeNode(43);
        TreeNode n78 = new TreeNode(78);
    
        TreeNode n25 = new TreeNode(25, n10, n34);
        TreeNode n65 = new TreeNode(65, n43, n78);
        TreeNode root = new TreeNode(50, n25, n65);
      private static class TreeNode {
        int value;
        TreeNode left;
        TreeNode right;
    
        TreeNode(int data) {
          this.value = data;
        }
    
        TreeNode(int data, TreeNode left, TreeNode right) {
          this.value = data;
          this.left = left;
          this.right = right;
        }
    
      }
      public static void printLeafNodesOfbt(TreeNode node) {
        // base case
        if (node == null) {
          return;
        }
    
        if (node.left == null && node.right == null) {
          System.out.printf("%d ", node.value);
        }
    
        printLeafNodesOfBT(node.left);
        printLeafNodesOfBT(node.right);
      }
    
      /**
       * Java method to print leaf nodes using iteration
       * 
       * @param root
       * 
       */
      public static void printLeafNodesOfBT(TreeNode root) {
    
        if (root == null) {
          return;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
    
        while (!stack.isEmpty()) {
          TreeNode node = stack.pop();
    
          if (node.right != null) {
            stack.add(node.right);
          }
    
          if (node.left != null) {
            stack.add(node.left);
          }
    
          if (node.left == null && node.right == null) {
            System.out.printf("%d ", node.value);
          }
    
        }
    
      }
    }
    
    

    Output:-

    10 34 43 78 
    10 34 43 78 

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: