public void connect(TreeLinkNode root) {
        TreeLinkNode p = root;
        TreeLinkNode first = null;

        while (p != null) {
            if (first == null) {
                first = p.left;
            }

            if (p.left != null) {
                p.left.next = p.right;
            } else {
                break;
            }
            if (p.next != null) {
                p.right.next = p.next.left;
                p = p.next;
            } else {
                p = first;
                first = null;
            }
        }
    }