import java.util.*;

public class Solution {
    public void connect(TreeLinkNode root) {
        if(root==null)
            return ;
        Queue<TreeLinkNode> q = new LinkedList<>();
        q.offer(root);

        while(!q.isEmpty()){
            int size = q.size();
            int t = 0;
            while(t < size){
                TreeLinkNode temp =  q.remove();
                if(temp.left!=null)    q.offer(temp.left);
                if(temp.right!=null)    q.offer(temp.right);
                if(t == size - 1)
                    temp.next = null;
                else
                    temp.next = q.peek();
                t++;
            }
        }
    }
}