We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
But that seems wasteful to me, because you're calling the function first, then checking to see if there's anything to call it on. So for every leaf node you're calling the function twice for no reason, and for every node with only one child you're calling it once for no reason. Why not check before calling, like this:
Tree: Postorder Traversal
You are viewing a single comment's thread. Return to all comments →
OK, I see everyone is writing this:
But that seems wasteful to me, because you're calling the function first, then checking to see if there's anything to call it on. So for every leaf node you're calling the function twice for no reason, and for every node with only one child you're calling it once for no reason. Why not check before calling, like this:
Can anyone tell me why the first way is preferable?