```
public void connect(TreeLinkNode root) {
if(root==null) return;
TreeLinkNode current = root;
TreeLinkNode nextLevel = current.left;
while(current.left!=null) {
nextLevel = current.left;
while(current!=null) {
current.left.next=current.right;
if(current.next!=null) current.right.next=current.next.left;
current = current.next;
}
current = nextLevel;
}
}
```

- current points to the current node on the current level
- nextLevel points to the first node of the next level
- while current!=null connect the children of the current node
- if current has next connect the current right children with the

current.next left child
- if current has next update current to current.next
- when the current level has no more nodes (current==null), point

current to the first node of the next level and restart the loop

(current = nextLevel)