May be template for python3 is not available , so solve in py 2.7


#1

class Solution:
# @param root, a tree node
# @return nothing
def connect(self, root):
stack=[]

    if root :
        stack.append(root)
    while stack:
        popping=[]
        while stack:
            node=stack.pop(0)
            if stack:
                node.next=stack[0]
            if node.left:
                popping.append(node.left)
            if node.right:
                popping.append(node.right)
        stack=popping

        
           
    return root