<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> class TreeNode : <EOL> def __init__ ( self , x ) : <EOL> self . val = x <EOL> self . left = None <EOL> self . right = None <EOL> self . next = None <EOL> class Solution : <EOL> def connect ( self , root ) : <EOL> """<STR_LIT>""" <EOL> if not root : <EOL> return None <EOL> q = [ root ] <EOL> while q : <EOL> current_level = q <EOL> q = [ ] <EOL> for ind , val in enumerate ( current_level ) : <EOL> if val . left : q . append ( val . left ) <EOL> if val . right : q . append ( val . right ) <EOL> try : <EOL> val . next = current_level [ ind + <NUM_LIT:1> ] <EOL> except IndexError : <EOL> val . next = None </s>
