---
title: Three bijections for marked walks
slug: three-bijections-for-marked-walks
date: 2026-10-20
description: Explicit bijections for three marked-walk identities, with constructions, inverses, and examples.
tags: [combinatorics, bijections, dyck-path, catalan, walk, oeis]
---
This post gives explicit bijections for the three marked-walk identities posed in item 3 of the [original question][question]. In all three cases, one side counts walks with marks that the other side moves or removes, and each cuts the walk at a position that can be found again by scanning for the first or last time some event happens, such as the first visit to a given height or the last time the walk reaches its maximum height. We will discuss the identities out of asked order, and in order of how much has to be recovered from the output, from a single hidden cut in Section 1 to an output with no mark at all in Section 3.
The same post, without the interactive diagrams, is also [my answer on Math.StackExchange][mse].
Throughout, we write $0$ for an up-step ($+1$) and $1$ for a down-step ($-1$). By a *balanced* walk we mean a walk that ends at height zero (though it is free to dip below zero along the way). Marks sit on the vertices of a walk: writing $A\mid B$ means we have placed a mark at time $|A|$, which splits the walk in two. Marks at the endpoints are allowed, and they still split the walk in two (one piece being the empty walk). Similarly, two marks divide the walk into three pieces, and are allowed to coincide (in the coinciding case, we have the middle piece be the empty walk).
## 1. Marked returns and the convolution identity
Let $e = N \bmod 2$, so that a walk of length $N$ ending at height $e$ is balanced when $N$ is even and ends one step above zero when $N$ is odd. We want to understand why the number of walks of length $N$ ending at height $e$ with a marked visit to height $h$ equals the number of walks of length $N$ ending at height $2h-e$ with a marked visit to height $0$.
### Moving a balanced piece to the front
In a target walk, the mark sits at height $0$, so the part before the mark is a balanced walk of some even length $2i$, counted by $\binom{2i}{i}$. To produce such a walk from an input, we would like to find a balanced piece inside the input and move it to the front.
Write the input as $A\mid B$, so that $A$ runs from height $0$ to the marked height $h$ and $B$ runs from $h$ to $e$. Whenever $A$ visits height $h$, the stretch of $A$ from that visit to the mark starts and ends at height $h$. Every visit to $h$ therefore gives a candidate balanced piece (including the visit at the mark itself, whose piece is empty), and the construction has to choose one of them. Cutting $A$ at its first visit to $h$, we write
$$
A = FE,
$$
where $F$ runs from $0$ to $h$ and reaches $h$ for the **first** time at its final vertex, and $E$ starts and ends at height $h$. We write the walk as $FE \mid B$, and then transform to $E \mid FB$, moving $E$ to the front along with the mark.
After the mark, $F$ runs from $0$ to $h$, and $B$ would then bring the walk to $e$, a net displacement of $e-h$ for $B$. However, the target walk must land at $2h-e$, so after $F$ it must move by $h-e$ instead. We solve this problem by flipping the up and down steps of $B$, that is, by taking its bitwise complement $\overline{B}$. Thus, the final map is
$$
\boxed{FE\mid B\longmapsto E\mid F\overline B.}
$$
Since $E$ is balanced, the mark sits at height $0$. After the mark, $F$ moves from $0$ to $h$ and $\overline{B}$ moves by $h-e$, so the output ends at $h + (h-e) = 2h-e$.
### Recovering the cut
Suppose we are handed an output walk $E\mid R$ ending at height $d = 2h-e$. The mark tells us where $E$ ends, and the endpoint tells us the original height, $h = (d+e)/2$. What remains is to split $R$ back into $F$ and $\overline{B}$. The walk $R$ starts at height $0$ and may pass through height $h$ many times, and $F$ ends at one of these visits. This is why cutting $A$ at the _first_ visit to $h$ was important, because now we can determine that $F$ ends at the first time $R$ reaches $h$, and scanning $R$ forward from the mark will recover it. Writing $R = FC$ at that first visit, the inverse returns $FE\mid\overline{C}$, where $\overline{C}$ is the walk we get after flipping the up and down steps of $C$.
$$
\boxed{E\mid F C \longmapsto F E\mid \overline C.}
$$
For an arbitrary target walk $E\mid R$ ending at $d$, this first visit to $h=(d+e)/2$ always exists, because $h$ lies between $0$ and $d$ (because $d \equiv e \pmod 2$ with $e \in \{0,1\}$), and a walk with $\pm 1$ steps cannot get from $0$ to $d$ without passing through $h$.
We can quickly verify that the reassembled object is a valid input and that the two maps undo each other. The prefix $FE$ ends at height $h$, and $\overline{C}$ has displacement $-(d-h) = h-d = e-h$, so $FE\mid\overline{C}$ ends at height $h + (e-h) = e$, as an input must. Since $E$ is balanced and begins at height $h$, the first visit of $FE$ to $h$ is still the end of $F$, so the forward map cuts $FE$ at the same place and returns $E\mid F\,C$, the walk we started with. Conversely, on an output $E\mid F\overline{B}$ of the forward map, the first-visit rule recovers the original $F$ and $C=\overline{B}$, and hence the original input.
In the diagram below, dragging the mark to another visit to the same height leaves $F$ alone and changes only $E$.
### Connection to the convolution
If we re-index by $k$ ($0 \le k \le N$) so that $T(N,k)$ counts input walks marked at height $h = k - \lfloor N/2 \rfloor = k - (N-e)/2$, then the output walk ends at height $d = 2k - N$. When the zero mark in the output occurs at step $2i$, there are $\binom{2i}{i}$ choices for the location of up-steps of the balanced prefix $E$ and $\binom{N-2i}{k-i}$ choices for the remaining $N-2i$ steps (which must contain $k-i$ up-steps to reach $2k-N$). Summing over $i$ gives
$$
T(N,k)=\sum_{i=0}^{\min(k,N-k)}
\binom{2i}{i}\binom{N-2i}{k-i},
$$
which is the convolution entry [OEIS A204621](https://oeis.org/A204621).
---
## 2. Pairs of zero marks
Next, we want a bijection between balanced walks of length $2n$ carrying two ordered marks at height zero (which may coincide) and balanced walks of length $2n$ carrying a single mark at an arbitrary vertex. Since two zero marks split a balanced walk into three independent balanced walks of lengths $2a, 2b, 2c$, this proves
$$
(2n+1)\binom{2n}{n}=
\sum_{a+b+c=n}
\binom{2a}{a}\binom{2b}{b}\binom{2c}{c}.
$$
### Excursions
Write an input walk with two zero marks as $L\mid K\mid R$, where $L$, $K$, and $R$ are balanced pieces. The output may carry only one mark, and both ends of $K$ have to be recovered from it. This is possible if we rearrange $K$ in a way that encodes the added information.
Let us rearrange $K$ into an *excursion*, a *nonempty* balanced walk that touches height zero only at its two endpoints. This is worth the trouble because, from any interior vertex of an excursion, the two endpoints are the nearest visits to height 0 on either side, so they give a natural way to turn a single mark back into two marks at height 0. Once $K$ is an excursion and the output mark is placed inside it, scanning outward from the mark recovers $L$, the excursion, and $R$.
The position of the mark within the excursion must also uniquely encode how $K$ was rearranged, so that the rearrangement can be undone. An excursion of length $2m$ has $2m-1$ interior vertices, so each excursion must arise from $2m-1$ choices of $K$, one for each position of the mark.
### Rotating the middle piece
Suppose $K$ is nonempty, say of length $2m > 0$, and that it starts with an up-step $0$. (We return at the end of the section to the empty case and to the case where $K$ starts with a down-step $1$, which is the same construction with all signs reversed.)
We set aside the initial up-step $0$ of $K$ for now. It will come back as the final step. The word $w$ that remains has length $2m-1$ and net displacement $-1$. A *cyclic permutation* of $w$ cuts it into two pieces $w = AB$ and swaps them to give $BA$. There are $2m-1$ of them, one for each place to cut, since cutting at either end gives $w$ back.
We cut $w$ at its **last** highest point. Let that maximum height be $z$, measured in $K$. Since $w$ has lost the initial up-step, the cut sits at height $z-1$ in $w$, and the piece from there to the end of $w$, at $-1$, has a net displacement of $-z$. Let us call this piece $B$, and the piece before the cut $A$, with a displacement $z-1$, so that $K = 0AB$ (where $A$ may be empty). $B$ never goes back up to $z-1$, because we cut at the *last* visit to the maximum. In $w$, $A$ climbed from $0$ and never rose above $z-1$, but if we keep $B$ at its heights in $w$, from $z-1$ down to $-1$, then in $BA$ the piece $A$ starts where $B$ ends, at $-1$, so every height is one lower and $A$ never reaches $z-1$ again either. Thus, if we move $B$ to start at $0$, the walk $BA$ stays strictly below zero after its first vertex and ends at height $(-z+(z-1))=-1$, and the reserved up-step $0$ climbs back to height $0$. The walk $BA0$ is a negative excursion, a walk that starts at height $0$, ends at height $0$, and is at a negative height everywhere in between.
Cutting at an earlier visit to the maximum will not work, as the cyclic permutation reaches the last visit at the same height. Cutting below the maximum fails too, since the cyclic permutation eventually passes the maximum. Even if the maximum only comes in the part moved from the front of $w$, where every height is one unit lower, it is still at least as high as where we started. This is the [cycle lemma][cycle]: a word with net displacement $-1$ ($m-1$ up-steps and $m$ down-steps) has exactly one cyclic permutation whose nonempty prefixes all lie strictly below zero.
We place the output mark between $B$ and $A$, writing $B\mid A0$, so the mark records where $w$ was cut. The $2m-1$ possible cuts correspond to the $2m-1$ interior vertices of the excursion, which is the count required above. Since $B$ ends at height $-z$, the mark sits at height $-z < 0$.
With the outer pieces restored, the map is
$$
\boxed{L\mid 0AB\mid R\longmapsto LB\mid A0R.}
$$
For example, consider $\mid001011\mid$, with $L$ and $R$ empty. The walk $001011$ reaches its maximum height $z=2$ at steps $2$ and $4$. Cutting at the last of these (step $4$) gives $0A = 0010$, so $A = 010$ and $B = 11$. The map sends
$$
\mid0\,(010)\,(11)\mid
\longmapsto
11\mid0100.
$$

```latex
\documentclass[tikz,border=6pt]{standalone}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usetikzlibrary{arrows.meta,decorations.pathreplacing,shapes.geometric}
\definecolor{ink}{HTML}{100F0F}
\definecolor{pieceA}{HTML}{1A4F8C}
\definecolor{pieceB}{HTML}{536907}
\definecolor{firststep}{HTML}{9D4310}
\tikzset{
walk/.style={line width=1.35pt,line cap=round,line join=round},
axis/.style={draw=black!40,line width=.35pt},
cut/.style={draw=black!50,densely dashed,line width=.45pt},
vertex mark/.style={diamond,draw=ink,fill=white,inner sep=0pt,minimum size=5pt,line width=.9pt},
piece brace/.style={decorate,decoration={brace,mirror,amplitude=3pt},line width=.6pt}
}
\newcommand{\axes}{%
\draw[axis] (0,-2.2)--(0,2.25);
\draw[axis] (-.12,0)--(6.25,0);
\foreach \h in {-2,-1,0,1,2}{
\draw[axis] (-.07,\h)--(.07,\h);
\node[anchor=east,font=\scriptsize,text=ink] at (-.16,\h) {$\h$};
}
\node[anchor=south west,font=\scriptsize,text=ink] at (0,2.3) {height};
\node[anchor=east,font=\small,text=ink] at (-.16,-3.2) {bits};
}
\begin{document}
\begin{tikzpicture}[x=.74cm,y=.74cm,text=ink]
\begin{scope}
\node at (3,3.8) {$\mid 0AB\mid$};
\axes
\draw[cut] (4,-.12)--(4,2.25);
\draw[walk,firststep] (0,0)--(1,1);
\draw[walk,pieceA] (1,1)--(2,2)--(3,1)--(4,2);
\draw[walk,pieceB] (4,2)--(5,1)--(6,0);
\foreach \p in {(0,0),(1,1),(2,2),(3,1),(4,2),(5,1),(6,0)}
\fill[ink] \p circle[radius=1.15pt];
\node[vertex mark] at (0,0) {};
\node[vertex mark] at (6,0) {};
\draw[ink,line width=.65pt] (4,2) circle[radius=3pt];
\node[anchor=south,font=\small] at (4.25,2.8) {last maximum ($t=4$)};
\draw[-{Stealth[length=3pt]},ink,line width=.45pt] (4.25,2.72)--(4.04,2.17);
\foreach \x/\bit/\col in {.5/0/firststep,1.5/0/pieceA,2.5/1/pieceA,3.5/0/pieceA,4.5/1/pieceB,5.5/1/pieceB}
\node[font=\small,text=\col] at (\x,-3.2) {$\bit$};
\draw[piece brace,firststep] (0,-3.6)--(1,-3.6) node[midway,below=6pt,text=firststep] {$0$};
\draw[piece brace,pieceA] (1,-3.6)--(4,-3.6) node[midway,below=6pt,text=pieceA] {$A$};
\draw[piece brace,pieceB] (4,-3.6)--(6,-3.6) node[midway,below=6pt,text=pieceB] {$B$};
\end{scope}
\node[font=\Large] at (7.5,0) {$\longmapsto$};
\begin{scope}[shift={(9,0)}]
\node at (3,3.8) {$B\mid A0$};
\axes
\draw[cut] (2,-2.2)--(2,.12);
\draw[walk,pieceB] (0,0)--(1,-1)--(2,-2);
\draw[walk,pieceA] (2,-2)--(3,-1)--(4,-2)--(5,-1);
\draw[walk,firststep] (5,-1)--(6,0);
\foreach \p in {(0,0),(1,-1),(2,-2),(3,-1),(4,-2),(5,-1),(6,0)}
\fill[ink] \p circle[radius=1.15pt];
\node[vertex mark] at (2,-2) {};
\node[anchor=north,font=\small] at (2,-2.35) {$-z=-2$};
\foreach \x/\bit/\col in {.5/1/pieceB,1.5/1/pieceB,2.5/0/pieceA,3.5/1/pieceA,4.5/0/pieceA,5.5/0/firststep}
\node[font=\small,text=\col] at (\x,-3.2) {$\bit$};
\draw[piece brace,pieceB] (0,-3.6)--(2,-3.6) node[midway,below=6pt,text=pieceB] {$B$};
\draw[piece brace,pieceA] (2,-3.6)--(5,-3.6) node[midway,below=6pt,text=pieceA] {$A$};
\draw[piece brace,firststep] (5,-3.6)--(6,-3.6) node[midway,below=6pt,text=firststep] {$0$};
\end{scope}
\end{tikzpicture}
\end{document}
```
*Here $z=2$. Moving $B$ to the front shifts it down by $2$, while placing $A$ after $B$ shifts $A$ down by $3$. The single mark sits at their join at height $-2$, and the initial up-step becomes the final return to zero.*
### Inverting the excursion map
Suppose the single mark in a balanced walk sits at some negative height $-z < 0$. Scanning outward from the mark, the nearest vertices at height zero bound a negative excursion, and the last step of that excursion is an up-step $0$. The mark and this final step split the excursion uniquely as $B\mid A0$, and the inverse map is
$$
\boxed{LB\mid A0R\longmapsto L\mid 0AB\mid R.}
$$
To verify that this works for every marked negative excursion $B\mid A0$ with the mark at height $-z < 0$, we trace the heights of the reassembled walk $0AB$. The initial $0$ reaches height $1$. The piece $A$, which ran from $-z$ to $-1$ while staying at or below $-1$, is shifted up by $z+1$, so $0A$ stays at or below $z$ and ends at height $z$. Then $B$, which ran from $0$ to $-z$ and stayed strictly below $0$ after its start, is appended at height $z$, so it stays strictly below $z$ after the join and ends at $0$. Consequently $0A$ ends at the last maximum of $0AB$, and the forward map cuts $0AB$ at the same place.
When $K$ starts with a down-step $1$, the same construction with signs reversed cuts $K = 1AB$ at its last minimum and maps
$$
\boxed{L\mid1AB\mid R\longmapsto LB\mid A1R,}
$$
which produces a positive excursion with its mark at height $+z > 0$. For the reverse map, the single mark lies at a positive height and the original walk can be found similarly. In both nonempty cases, the rearrangement $sAB \to BAs$ is the same: a cyclic permutation of $w = AB$, with the set-aside first step $s$ moved to the end.
That leaves the case where the two zero marks coincide, so that $K$ is the empty walk. The forward map merges them into a single mark at height $0$, sending $L\mid\,\mid R \mapsto L\mid R$. A mark at height $0$ can only come from this case, because the nonempty cases put the mark at $-z < 0$ or $+z > 0$. So the inverse splits a mark at height $0$ back into two coincident zero marks.
In the diagram below, the single output mark lands at height $0$ only when the two zero marks are dragged together.
### Bonus: The Return of the Catalans
The count from the start of this section becomes an identity if we restrict the construction to a single middle piece $K$ of length $2m > 0$ starting with up-step $0$. The construction then gives a bijection between balanced walks of length $2m$ starting with up-step $0$, of which there are $\binom{2m-1}{m-1}$, and negative excursions of length $2m$ with one interior mark. Reflecting a negative excursion of length $2m$ (about the x-axis) and stripping its first and last steps produces a Dyck path of length $2m-2$, so there are $C_{m-1} = \frac{1}{m}\binom{2m-2}{m-1}$ such excursions, each with $2m-1$ interior vertices available to mark. Equating the two counts gives the classical identity
$$
\binom{2m-1}{m-1}=(2m-1)C_{m-1}.
$$
---
## 3. Marks at a fixed height
For the last identity, we want a bijection between balanced walks of length $2n$ with a marked visit to a fixed height $k$, and binary strings of length $2n+1$ containing at most $n-|k|$ ones. Such a bijection gives a combinatorial proof that
$$
\#\{\text{balanced walks with a marked visit to }k\}
=\sum_{r=0}^{n-|k|}\binom{2n+1}{r}.
$$
We assume $|k|\le n$, since otherwise both sets are empty.
### Inserting a step at the mark
In this identity the output is an unmarked string, one step longer than the input, so the extra step can be inserted at the mark. An [answer][answer] by natalia to an earlier question proves this sum as its equation (2) and credits GChromodynamics for help with the proof. That proof inserts a step after the marked visit to $k$ and reflects every step after it, which turns the marked visits to $k$, minus the marked visits to $k+1$, into walks of length $2n+1$ ending at height $2k+1$; the sum then follows by adding up these differences. Here we are aiming for a single bijection, so the inserted step has to be recognizable as the first or last occurrence of some event, like the first visit to $h$ in Section 1 or the last maximum in Section 2.
Suppose first that $k\ge0$, and write the marked walk as $u\mid v$, so that $u$ ends at height $k$. If we insert an up-step $0$ at the mark, the new step goes from height $k$ to $k+1$. For the inverse to be able to find it, we want it to be the *last* up-step from $k$ to $k+1$ in the whole walk. That will always be true if the walk never comes back down to height $k$ afterwards, but the suffix $v$ does not satisfy this condition as it stands, since it has to get from height $k$ back to $0$. So we need to modify $v$ into a walk $v'$ that stays at or above its starting height, in a way that can be undone.
As a running example, take $n=6$, $k=1$, and the walk $01001\mid0110110$.
The simplest change to $v$ would be to reflect all of it, swapping every $0$ and $1$. This is the idea of the proof cited above, and it sends every marked walk to a string ending at height $2k+1$, that is, with exactly $n-k$ ones. But there are only $\binom{2n+1}{n-k}$ such strings, far fewer than the marked walks. For $n=6$ and $k=1$, the values of our running example, there are $\binom{13}{5}=1287$ strings of length $13$ with five ones, but $\sum_{r=0}^{5}\binom{13}{r}=2380$ marked walks, nearly twice as many. Reflection must therefore send different marked walks to the same string, and the inverse could not tell them apart. A bijection onto strings with every number of ones up to $n-k$ has to keep a record, and the number of ones can be that record if we choose which steps to flip in a way the inverse can recognize.
### Lifting the suffix
Which steps do we need to flip? Read $v$ as a string of parentheses, with each up-step $0$ as "(" and each down-step $1$ as ")", and match each $1$ with the most recent unmatched $0$ before it. A matched $0$ and $1$, together with everything between them, return to the height where they started without going below it, so no matched part of $v$ takes the walk to a new low. The only steps that can are the unmatched down-steps: each lands at a height the walk has not visited before, so these are its record-low down-steps. If we turn every unmatched $1$ into a $0$ and leave the rest alone, nothing pulls the walk down any more, and it stays at or above its starting height. The inverse can run the same matching to find these steps again.
On our example suffix $v = 0110110$, scanning from left to right, the matching gives
$$
0110110=
\underbrace{01}_{\text{matched}}\;\boxed{1}\;
\underbrace{01}_{\text{matched}}\;\boxed{1}\;\boxed{0},
$$
where the boxed bits are left unmatched. In general, any unmatched $0$ preceding an unmatched $1$ would have been matched with it, so the unmatched bits always form a subsequence of the form
$$
1^a0^b
$$
(in our example, $110$, so $a=2$ and $b=1$). Moreover, no matched pair can straddle an unmatched bit: an enclosed $0$ would have to be matched before the outer pair could close, and an enclosed $1$ would have found the outer opening $0$ available. So the segments before, between, and after the unmatched bits are balanced detours, each staying at or above its own starting height.
We now flip those $a$ unmatched ones to zeros and leave every matched bit and every unmatched zero as it was, making the unmatched subsequence $0^{a+b}$. Call the result $v'$. The balanced detours are now joined only by up-steps, so $v'$ never dips below its starting height. (Up to our reversed bit convention, this is the *Catalan factorization* of [Eğecioğlu and King, §3][catalan].)
Inserting the up-step $0$ at the mark before $v'$, the full forward map is
$$
\boxed{u\mid v\longmapsto u\,0\,v'.}
$$
In our running example, flipping the two boxed ones in $v$ gives $v'=0100100$, so
$$
01001\mid0110110\longmapsto(01001)\,0\,(0100100).
$$

```latex
\documentclass[tikz,border=6pt]{standalone}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\definecolor{ink}{HTML}{100F0F}
\definecolor{changed}{HTML}{9D4310}
\tikzset{
walk/.style={draw=ink,line width=1pt,line cap=round,line join=round},
changed step/.style={draw=changed,line width=1.6pt,line cap=round},
axis/.style={draw=black!40,line width=.35pt},
excursion base/.style={draw=black!45,densely dashed,line width=.4pt}
}
\newcommand{\axes}{%
\draw[axis] (0,-2.25)--(0,3.25);
\draw[axis] (-.12,0)--(7.25,0);
\foreach \h in {-2,-1,0,1,2,3}{
\draw[axis] (-.08,\h)--(.08,\h);
\node[anchor=east,font=\scriptsize,text=ink] at (-.18,\h) {$\h$};
}
\node[anchor=south west,font=\scriptsize,text=ink] at (0,3.3) {height};
\node[anchor=east,font=\small,text=ink] at (-.18,-2.95) {bits};
}
\begin{document}
\begin{tikzpicture}[x=.68cm,y=.68cm,text=ink]
\begin{scope}
\node at (3.5,4.05) {$v=0110110$};
\fill[black!13] (0,0)--(1,1)--(2,0)--cycle;
\fill[black!13] (3,-1)--(4,0)--(5,-1)--cycle;
\axes
\draw[excursion base] (3,-1)--(5,-1);
\draw[walk] (0,0)--(1,1)--(2,0)--(3,-1)--(4,0)--(5,-1)--(6,-2)--(7,-1);
\draw[changed step] (2,0)--(3,-1);
\draw[changed step] (5,-1)--(6,-2);
\foreach \p in {(0,0),(1,1),(2,0),(3,-1),(4,0),(5,-1),(6,-2),(7,-1)}
\fill[ink] \p circle[radius=1.15pt];
\foreach \x/\bit/\col in {.5/0/ink,1.5/1/ink,2.5/1/changed,3.5/0/ink,4.5/1/ink,5.5/1/changed,6.5/0/ink}
\node[font=\small,text=\col] at (\x,-2.95) {$\bit$};
\end{scope}
\node[font=\Large] at (8.5,.5) {$\longmapsto$};
\begin{scope}[shift={(10,0)}]
\node at (3.5,4.05) {$v'=0100100$};
\fill[black!13] (0,0)--(1,1)--(2,0)--cycle;
\fill[black!13] (3,1)--(4,2)--(5,1)--cycle;
\axes
\draw[excursion base] (3,1)--(5,1);
\draw[walk] (0,0)--(1,1)--(2,0)--(3,1)--(4,2)--(5,1)--(6,2)--(7,3);
\draw[changed step] (2,0)--(3,1);
\draw[changed step] (5,1)--(6,2);
\foreach \p in {(0,0),(1,1),(2,0),(3,1),(4,2),(5,1),(6,2),(7,3)}
\fill[ink] \p circle[radius=1.15pt];
\foreach \x/\bit/\col in {.5/0/ink,1.5/1/ink,2.5/0/changed,3.5/0/ink,4.5/1/ink,5.5/0/changed,6.5/0/ink}
\node[font=\small,text=\col] at (\x,-2.95) {$\bit$};
\end{scope}
\end{tikzpicture}
\end{document}
```
*Both panels plot the suffix starting at height zero. The shaded balanced detours keep their shapes and horizontal positions, while the two highlighted down-steps flip into up-steps.*
Since $u$ ends at height $k$ and the whole walk $uv$ is balanced, the suffix $v$ has net displacement $-k$, which means $a-b=k$, and in particular $a \ge k$. The input has $n$ ones, and we flipped $a$ of them, so the output has $n-a\le n-k$ ones. The number of ones in the output thus records how many steps were flipped, which is why the target set contains strings with every number of ones up to $n-k$. Finally, the inserted up-step at the mark moves from $k$ to $k+1$, and $v'$ stays at or above $k+1$ after it, so the end of $u$ is the last time the output visits height $k$.
### Inverting the map
Now suppose we are given $k \ge 0$ and a binary string $q$ of length $2n+1$ with $r \le n-k$ ones. The forward map reduced the number of ones from $n$ to $n-a$, so the number of bits to flip back is
$$
a=n-r.
$$
Since $r \le n-k$, we have $a \ge k$, so $q$ ends at height
$$
(2n+1-r) - r = 2n+1-2r = 2a+1 > k.
$$
In our example output, $q = 0100100100100$, there are $r=4$ ones, so $a = 6-4 = 2$ unmatched zeros must be flipped back into ones.
To locate the cut, we find the last time $q$ visits height $k$ and factor $q$ there as $q=u\,0\,v'$. Since $q$ ends above $k$, the step after that visit has to be an up-step to $k+1$, and it is the step the forward map inserted. In our example, $q$ is last at height $1$ after step $5$, which recovers $u=01001$ and $v'=0100100$.
To decide which zeros of $v'$ to flip back, we run parenthesis matching on $v'$. In the original suffix $v$, the unmatched bits had the form $1^a0^b$, with all $a$ ones before all $b$ zeros, and flipping the ones turned this subsequence into $0^{a+b}$. The flipped bits are therefore the *first* $a$ unmatched zeros of $v'$. The inverse turns those back into ones, and replaces the $0$ between $u$ and $v'$ with the mark. On our example suffix, matching gives
$$
v'=\underbrace{01}_{\text{matched}}\;\boxed{0}\;
\underbrace{01}_{\text{matched}}\;\boxed{0}\;\boxed{0},
$$
and flipping the first two boxed zeros restores $v=0110110$ and the marked walk $01001\mid0110110$.
We still need to check that this inverse is defined on every string $q$ with $r \le n-k$ ones. Since $q$ starts at $0 \le k$ and ends above $k$, it visits height $k$, and after its last visit the suffix $v'$ stays at or above its starting height $k+1$. Every $1$ in $v'$ is therefore matched by a $0$ in $v'$, so $v'$ has no unmatched ones, and the number of unmatched zeros in $v'$ equals its net displacement:
$$
(2a+1)-(k+1)=2a-k\ge a.
$$
So $v'$ always has at least $a$ unmatched zeros available to flip. Flipping the first $a$ of them changes the unmatched subsequence from $0^{2a-k}$ to $1^a0^{a-k}$, which gives a suffix $v$ of net displacement $-k$ that brings the walk from height $k$ back to $0$. Because the balanced detours between unmatched bits are never touched, matching the bits of $v$ matches the same bits as matching the bits of $v'$, and its unmatched ones are the $a$ bits that the inverse flipped. The forward map therefore flips those bits back and reinserts the step that the inverse removed, returning $q$.
Finally, if $k<0$, we complement every bit of the input walk, which reflects it vertically so that the mark sits at height $|k| > 0$. We then apply the construction for $|k|$, and the inverse complements the recovered walk at the end. (When $n=k=0$, the empty marked walk maps to $0$.)
In the diagram below, wherever the mark is dragged, the inserted up-step is the last step from $k$ to $k+1$ in the output.
## Construction notes
Before landing on the cyclic-permutation construction in Section 2, my initial proof composed the bijection from Section 1 with two operations from [Eğecioğlu and King, §§2–3][catalan] (splitting at the last zero and converting a non-returning walk into a balanced walk). That route worked, but inverting it required chasing down several intermediate cuts. Asking whether a single interior mark could encode both endpoints of $K$ at once led to excursions and the cycle lemma.
## Acknowledgements
Thank you [natalia](https://math.stackexchange.com/users/1769503/natalia) for bringing my attention to these questions! Thank you [Mike Earnest](https://math.stackexchange.com/users/177399/mike-earnest) for your linked answer, which led me to discover the ideas behind Catalan Factorizations!
## References
Ömer Eğecioğlu and Alastair King, [*Random Walks and Catalan Factorization*][catalan], *Congressus Numerantium* **138** (1999), 129–140.
Nachum Dershowitz and Shmuel Zaks, [*The Cycle Lemma and Some Applications*][cycle], *European Journal of Combinatorics* **11** (1990), 35–40, §1.
[question]: https://math.stackexchange.com/questions/5137608/
[answer]: https://math.stackexchange.com/a/5136947
[mse]: https://math.stackexchange.com/a/5152342
[catalan]: https://sites.cs.ucsb.edu/~omer/DOWNLOADABLE/randomcatalan99.pdf
[cycle]: https://www.cs.tau.ac.il/~nachumd/papers/CL.pdf