```
def connect(self, root):
if not root: return
queue=[root]
while queue:
level=[]
for _ in range(len(queue)):
node = queue.pop(0)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
level.append(node)
for i in range(len(level)-1):
level[i].next=level[i+1]
level[-1].next = None
```