```
def connect(self, root):
queue = []
depth = -1
if root:
queue.append([root, depth+1])
while queue:
node, d = queue.pop(0)
if d - depth == 1:
prev = node
else:
node.next = prev
prev = node
depth = d
for child in [node.right, node.left]:
if child:
queue.append([child, d+1])
```