```
class Solution:
# @param root, a tree link node
# @return nothing
def connect(self, root):
left = root
cur = None
while left:
cur = left
while cur:
if cur.left:
cur.left.next = cur.right or self.find_next_node(cur.next)
if cur.right:
cur.right.next = self.find_next_node(cur.next)
cur = cur.next
left = self.find_next_node(left)
def find_next_node(self, node):
while node:
if node.left:
return node.left
if node.right:
return node.right
node = node.next
return None
```