Prasanna Raman wrote:Hello,
I've implemented this in 2 ways. One method is traversing the tree in level order and the other method uses recursion. Please let me know which one is better in terms of time and space and also if there is a better way of implementing this.
Level-order method:
Recursion:
Prasanna Raman wrote:I've implemented this in 2 ways. One method is traversing the tree in level order and the other method uses recursion. Please let me know which one is better in terms of time and space and also if there is a better way of implementing this.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Prasanna Raman wrote:
Thank you. Sorry, I was only trying to edit but clicked on the wrong option.
Coming to your response, I just read somewhere that this kind of recursion (since this is not tail recursive) takes up a lot of stack space and hence the iterative version might be better. Is that correct? If not, why is recursion better for trees? I see that the code is cleaner, but any other reason?
Prasanna Raman wrote:Thank you Henry. So in this case, what would be the running times and space complexity of the two? Does it take O(n) running time?
Tim Driven Development | Test until the fear goes away
Tim Cooke wrote: I have one question though: What is a BinTreeNode? Is this a custom Type you've written yourself?
Tim Driven Development | Test until the fear goes away
Consider Paul's rocket mass heater. |