in reply to Re: recursion
in thread Trouble traversing binary tree (was: recursion)

That's exactly what merlyn told you: you don't "localize" $head (I should see "lexicalize" maybe, but "localize" should be clearer... maybe :-). That way you overwrite the same $head variable again and again when you go down the tree and, when it finally reaches the leaf, it goes up... printing always the same values (count them: you printed one 19 per level).

One fundamental thing when you write recursive code is to properly localize the variables in play and never use global variables unless you are really sure you can't do without. If you use my, every call of the subroutine gets it's own clean $head, which is exactly what you want.

Even if Perl allows you not to declare variables, it doesn't mean you should do it. I learned on my own expenses that it's not safe to work without use strict, unless you are writing a one liner.

Ciao!
--bronto