public void connect(TreeLinkNode root) {
        if (root == null)
            return;
        Deque<TreeLinkNode> queue = new LinkedList<TreeLinkNode>();
        queue.push(root);
        while (!queue.isEmpty()) {
            TreeLinkNode node = queue.pollFirst();
            int size = queue.size();
            if (node.left != null)
                queue.offerLast(node.left);
            if (node.right != null)
                queue.offerLast(node.right);
            while (size-- > 0) {
                TreeLinkNode temp = queue.pollFirst();
                if (temp.left != null)
                    queue.offerLast(temp.left);
                if (temp.right != null)
                    queue.offerLast(temp.right);
                node.next = temp;
                node = temp;
            }
            node.next = null;
        }
    }