Check if all leaves are at same level | Python3 program to check if all leaf nodes are at same level of binary tree ; Tree Node returns 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 it 's leaf node ; if it 's first leaf node till now,  then update the result ; if it is not the first leaf node , then compare the level with level of previous leaf node ; Driver Code ; construct a tree
INT_MAX = 2 ** 31 NEW_LINE INT_MIN = - 2 ** 31 NEW_LINE class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def checkLevelLeafNode ( root ) : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT q = [ ] NEW_LINE q . append ( root ) NEW_LINE result = INT_MAX NEW_LINE level = 0 NEW_LINE while ( len ( q ) ) : NEW_LINE INDENT size = len ( q ) NEW_LINE level += 1 NEW_LINE while ( size > 0 or len ( q ) ) : NEW_LINE INDENT temp = q [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE if ( temp . left ) : NEW_LINE INDENT q . append ( temp . left ) NEW_LINE if ( not temp . left . right and not temp . left . left ) : NEW_LINE INDENT if ( result == INT_MAX ) : NEW_LINE INDENT result = level NEW_LINE DEDENT elif ( result != level ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT DEDENT if ( temp . right ) : NEW_LINE INDENT q . append ( temp . right ) NEW_LINE if ( not temp . right . left and not temp . right . right ) : NEW_LINE INDENT if ( result == INT_MAX ) : NEW_LINE INDENT result = level NEW_LINE DEDENT elif ( result != level ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT size -= 1 NEW_LINE DEDENT DEDENT DEDENT return 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 1 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 3 ) NEW_LINE root . left . right = newNode ( 4 ) NEW_LINE root . right . left = newNode ( 5 ) NEW_LINE root . right . right = newNode ( 6 ) NEW_LINE result = checkLevelLeafNode ( root ) NEW_LINE if ( result ) : NEW_LINE INDENT print ( " All ▁ leaf ▁ nodes ▁ are ▁ at ▁ same ▁ level " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Leaf ▁ nodes ▁ not ▁ at ▁ same ▁ level " ) NEW_LINE DEDENT DEDENT