The main problem is with the way you've implemented the recursion.
All variables declared in a function are local to the particular invocation of the function (unless declared global or persistent). Therefore, each time you call you recurse into tree2, a new set of k, a, p, and l is calculated. Since you don't return them at the end of tree2, their value is lost.
Thus, you need to change your function signature to:
function [a, b, p, l] = tree2(root); %I've changed k to b. Why was it called k anyway?
Then every time you invoke the recursion you add the return values to your local a, b, ...:
[branch_a, branch_b, branch_p, branch_l] = tree2(branch);
a = a + branch_a;
b = b + branch_b;
p = p + branch_p;
l = l + branch_l;
This also greatly simplifies your if elseif. You just add 1 to the relevant variable whenever you encounter a leaf/branch.
if Cn{i}(1) == 'b'
b = b+1;
elseif ...
This should answer your question. Now, there are a few odd things about your code:
1. I find it much easier to read
if ~exist('f')
than
if exist('f') == 0
they're equivalent, but the former I read if not exist. That carries the intent better.
In any case, f is a local variable, so it never exists at that point of the code, so you might as well get rid of the if.
I don't see the point of the while loop anyway. Particularly as it'll never end since f is always > 0 as it starts at 1 and increases from there.
branch = root.(Cn{i});
