Count full nodes in a Binary tree ( Iterative and Recursive ) | C # 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 code ; 2 / \ 7 5 \ \ 6 9 / \ / 1 11 4 Let us create Binary Tree as shown
using System ; using System . Collections . Generic ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = null ; right = null ; } } public class BinaryTree { Node root ; int getfullCount ( ) { if ( root == null ) return 0 ; Queue < Node > queue = new Queue < Node > ( ) ; queue . Enqueue ( root ) ; int count = 0 ; while ( queue . Count != 0 ) { Node temp = queue . Dequeue ( ) ; if ( temp . left != null && temp . right != null ) count ++ ; if ( temp . left != null ) { queue . Enqueue ( temp . left ) ; } if ( temp . right != null ) { queue . Enqueue ( 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 ) ; Console . WriteLine ( tree_level . getfullCount ( ) ) ; } }