Count full nodes in a Binary tree ( Iterative and Recursive ) | Java program to count full nodes in a Binary Tree using Iterative approach ; Class to represent Tree node ; Class to count full nodes of Tree ; Function to get the count of full Nodes in a binary tree ; If tree is empty ; Initialize empty queue . ; Do level order traversal starting from root ; Initialize count of full nodes ; Enqueue left child ; Enqueue right child ; Driver program ; 2 / \ 7 5 \ \ 6 9 / \ / 1 11 4 Let us create Binary Tree as shown
import java . util . Queue ; import java . util . LinkedList ; class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = null ; right = null ; } } class BinaryTree { Node root ; int getfullCount ( ) { if ( root == null ) return 0 ; Queue < Node > queue = new LinkedList < Node > ( ) ; queue . add ( root ) ; int count = 0 ; while ( ! queue . isEmpty ( ) ) { Node temp = queue . poll ( ) ; if ( temp . left != null && temp . right != null ) count ++ ; if ( temp . left != null ) { queue . add ( temp . left ) ; } if ( temp . right != null ) { queue . add ( temp . right ) ; } } return count ; } public static void main ( String args [ ] ) { BinaryTree tree_level = new BinaryTree ( ) ; tree_level . root = new Node ( 2 ) ; tree_level . root . left = new Node ( 7 ) ; tree_level . root . right = new Node ( 5 ) ; tree_level . root . left . right = new Node ( 6 ) ; tree_level . root . left . right . left = new Node ( 1 ) ; tree_level . root . left . right . right = new Node ( 11 ) ; tree_level . root . right . right = new Node ( 9 ) ; tree_level . root . right . right . left = new Node ( 4 ) ; System . out . println ( tree_level . getfullCount ( ) ) ; } }