```
# Definition for binary tree with next pointer.
# class TreeLinkNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
# self.next = None
class Solution:
# @param root, a tree link node
# @return nothing
# 12:23
def connect(self, root):
if not root:
return
levels = [[root]]
self.levelConnect(levels)
def levelConnect(self, levels):
while levels:
level = levels.pop(0)
newLevel = []
while level:
node = level.pop(0)
if len(level):
node.next = level[0]
if node.left:
newLevel.append(node.left)
if node.right:
newLevel.append(node.right)
if newLevel:
levels.append(newLevel)
```