Solution (source code)

= Solution

At a node with time $n-1$ price $s$, choose the stock holding
$$
\Delta_n(s)=\frac{V(n,s(1+b))-V(n,s(1+a))}{s(b-a)}.
$$
The two possible values of the stock position differ by exactly the difference between the two continuation claims. Choose the bank holding so that total wealth is $V(n-1,s)$. The pricing recursion makes its next value equal to the appropriate continuation value in both states. Backward induction from $V(N,s)=g(s)$ therefore replicates $g(S_N)$ from initial capital $V(0,S_0)$.