From top to button, level to level from left to right, just need to record the number of node in each level.

```
class Solution(object):
def connect(self, root):
if not root: return
stack=collections.deque([root])
while stack:
length=len(stack)
for i in xrange(length):
node=stack.popleft()
if node.left: stack+=node.left,
if node.right: stack+=node.right,
if i==length-1: node.next=None
else: node.next=stack[0]
```