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.
It already does the if statement you stated but it just does it implicitly on the recursive call. In terms of savings, you aren't really saving anything because the cost of you doing the if statements would be the trade-off for doing those function calls. Which means this really doesn't change the complexity of this algorithm at all which would still be Big-Oh(N).
The biggest caveat really is if you call root.left or root.right on an None Type root (i.e an empty tree) it would return an AttributeError which the first one would be able to handle.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Tree: Postorder Traversal
You are viewing a single comment's thread. Return to all comments →
It already does the if statement you stated but it just does it implicitly on the recursive call. In terms of savings, you aren't really saving anything because the cost of you doing the if statements would be the trade-off for doing those function calls. Which means this really doesn't change the complexity of this algorithm at all which would still be Big-Oh(N).
The biggest caveat really is if you call root.left or root.right on an None Type root (i.e an empty tree) it would return an AttributeError which the first one would be able to handle.