```
class Solution:
def connect(self, root):
dic = {}
def dfs(r, dep):
if not r:
return
if dep in dic:
dic[dep].next = r
r.next = None
dic[dep] = r
dfs(r.left, dep + 1)
dfs(r.right, dep + 1)
dfs(root, 0)
```