by jfmengels1 · 4y ago · 0 comments
As for walking a tree, I agree, but this optimization does not work well with trees shrug, making lists a nicer example in my opinion.
As a teaching method, I feel that factorial is a relatively good example because it is fairly easy to grok in your head or on paper. Recursing through a tree adds a lot of mental overhead, so I feel like it could be a follow-up example to factorial, but it's probably too hard as an initial example. Also, switching to a stack-safe function using recursion is again much easier for factorial than it would be for traversing a tree.