My Python 2.7 solution


#1

class Solution:
# @param root, a tree node
# @return nothing

def connect(self, root):
    node=root
    temp=None
    q=[]
    q.append(node)
    while(q):
        n=len(q)
        for i in range(0,n):
            prev=temp
            temp=q.pop(0)
            if(i>0):
                prev.next=temp
            if(temp.left is not None):
                q.append(temp.left)
            
            if(temp.right is not None):
                q.append(temp.right)
        temp.next=None