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

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

            if (p.left != null) {
                if (last != null) {
                    last.next = p.left;
                }
                last = p.left;
            }

            if (p.right != null) {
                if (last != null) {
                    last.next = p.right;
                }
                last = p.right;
            }

            if (p.next != null) {
                p = p.next;
            } else {
                p = first;
                first = null;
                last = null;
            }
        }
    }