function LadC = CRRladC(T,S0,K,L,r,v,N)
% Octave/MATLAB function to price a ladder Call
% option using a Cox-Ross-Rubinstein (CRR) model.
% INPUTS:                           (Example)
%   T  = expiration time in years   (1)
%   S0 = spot price                 (50)
%   K  = strike price               (55)
%   L  = increasing prices > S0,K   ([60,65,70])
%   r  = riskless yield per year    (0.05)
%   v  = volatility; must be >0     (0.20)
%   N  = height of the tree         (4)
% OUTPUTS:
%   LadC = Ladder Call option price array.
% EXAMPLE:
%   LadC=CRRladC(1,50,55,[60,65,70],0.05,0.20,4)
%
  [pu,up,R]=CRRparams(T,r,v,N);
  Sbar=NRTCRR(S0,up,1/up,N); % S in an NRT
  MaxS=NRTmax(Sbar,N); % non-recombining max tree
  LadC=zeros(size(Sbar)); % Call prices NRT
  % Initialize with the payoffs at expiry:
  k=length(L);     % number of ladder levels, >1
  for m=2^N:(2*2^N-1)  % state indexes at expiry
    if(MaxS(m)<L(1)) % below level L(1) is special
      LadC(m)=max(Sbar(m)-K,0);
    else % MaxS >= L(1)
      if(MaxS(m)<L(k)) % ...use levels L(2)<...<L(k)
        for l=2:k      % loop to find the level
          if(MaxS(m)<L(l))
            LadC(m)=max(max(Sbar(m)-K,L(l-1)-K),0);
            break; % found l: L(l-1)=<MaxS<L(l),
          end
        end       % ... so exit the "l" loop
      else % MaxS>=L(k), another special case
        LadC(m)=max(max(Sbar(m)-K,L(k)-K),0);
      end
    end
  end
  for m=(2^N-1):-1:1 % backward recursion
    LadC(m)=(pu*LadC(2*m+1)+(1-pu)*LadC(2*m))/R;
  end
  return % LadC(1) is the option premium
end
