Do level order traversal until we find an empty place .
while ( len ( q ) ) : NEW_LINE INDENT temp = q [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE if ( not temp . left ) : NEW_LINE INDENT temp . left = newNode ( key ) NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT q . append ( temp . left ) NEW_LINE DEDENT if ( not temp . right ) : NEW_LINE INDENT temp . right = newNode ( key ) NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT q . append ( temp . right ) NEW_LINE DEDENT DEDENT