Check if all leaves are at same level | Java program to check if all leaf nodes are at same level of binary tree ; User defined node class ; Constructor to create a new tree node ; return true if all leaf nodes are at same level , else false ; create a queue for level order traversal ; traverse until the queue is empty ; traverse for complete level ; check for left child ; if its leaf node ; if it 's first leaf node, then update result ; if it 's not first leaf node, then compare   the level with level of previous leaf node. ; check for right child ; if its leaf node ; if it 's first leaf node, then update result ; if it 's not first leaf node, then compare   the level with level of previous leaf node. ; Driver code ; construct a tree
import java . util . * ; class Node { int data ; Node left , right ; Node ( int key ) { int data = key ; left = right = null ; } } class GFG { static boolean checkLevelLeafNode ( Node root ) { if ( root == null ) return true ; Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; int result = Integer . MAX_VALUE ; int level = 0 ; while ( q . size ( ) != 0 ) { int size = q . size ( ) ; level ++ ; while ( size > 0 ) { Node temp = q . remove ( ) ; if ( temp . left != null ) { q . add ( temp . left ) ; if ( temp . left . left == null && temp . left . right == null ) { if ( result == Integer . MAX_VALUE ) result = level ; else if ( result != level ) return false ; } } if ( temp . right != null ) { q . add ( temp . right ) ; if ( temp . right . left == null && temp . right . right == null ) { if ( result == Integer . MAX_VALUE ) result = level ; else if ( result != level ) return false ; } } size -- ; } } return true ; } public static void main ( String args [ ] ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . right = new Node ( 4 ) ; root . right . left = new Node ( 5 ) ; root . right . right = new Node ( 6 ) ; boolean result = checkLevelLeafNode ( root ) ; if ( result == true ) System . out . println ( " All ▁ leaf ▁ nodes ▁ are ▁ at ▁ same ▁ level " ) ; else System . out . println ( " Leaf ▁ nodes ▁ not ▁ at ▁ same ▁ level " ) ; } }