Check whether a binary tree is a full binary tree or not | Iterative Approach | node class ; put in the data ; function to check whether a binary tree is a full binary tree or not ; if tree is empty ; queue used for level order traversal ; append ' root ' to ' q ' ; traverse all the nodes of the binary tree level by level until queue is empty ; get the pointer to ' node ' at front of queue ; if it is a leaf node then continue ; if either of the child is not None and the other one is None , then binary tree is not a full binary tee ; append left and right childs of ' node ' on to the queue ' q ' ; binary tree is a full binary tee ; Driver Code
class getNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def isFullBinaryTree ( root ) : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return True NEW_LINE DEDENT q = [ ] NEW_LINE q . append ( root ) NEW_LINE while ( not len ( q ) ) : NEW_LINE INDENT node = q [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE if ( node . left == None and node . right == None ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( node . left == None or node . right == None ) : NEW_LINE INDENT return False NEW_LINE DEDENT q . append ( node . left ) NEW_LINE q . append ( node . right ) NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = getNode ( 1 ) NEW_LINE root . left = getNode ( 2 ) NEW_LINE root . right = getNode ( 3 ) NEW_LINE root . left . left = getNode ( 4 ) NEW_LINE root . left . right = getNode ( 5 ) NEW_LINE if ( isFullBinaryTree ( root ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT