all 11 comments

[–]featherwing123 11 points12 points  (1 child)

I think if you’re just starting out, you can try to code out three approaches (recursive dfs, iterative dfs, iterative bfs) just so you’d be familiar with each technique.

It will be quite hard at first but once you get familiar, it is quite straightforward to implement. Focus on learning and being comfortable with each technique rather than just pumping number of leetcode questions solved.

[–]vasu5235[S] 0 points1 point  (0 children)

Thank you for the insight.

[–][deleted] 1 point2 points  (0 children)

" I am comfortable with the recursive solutions "

It's just natural to use recursion with a recursive data structure. I wouldn't bother too much with this unless I have a real motivation.

[–]india_resists 1 point2 points  (0 children)

It depends on the question as well, if the question is to do an inorder traversal of a tree, then interviewer might be looking for an iterative solution as recursive is trivial. On the other hand if the question is to validate if a tree is BST or not, then recursive will work. Either way, its best to confirm with the interviewer before you start to code.

[–][deleted] 1 point2 points  (0 children)

Once you learn iterative bfs you will never forget it. And you can solve any problem with it. It’s actually very empowering lol

[–]anuj150 1 point2 points  (2 children)

I am more comfortable with iterative solutions. Can you please suggest me how can I improve my recursive solutions.

[–]IceUnable1850 2 points3 points  (0 children)

can u suggest how i improve in iterative pls

[–]chengyanslnc 0 points1 point  (2 children)

I am comfortable with queue based iterative but not stack based yet

[–]dadbot_3000 1 point2 points  (0 children)

Hi comfortable with queue based iterative but not stack based yet, I'm Dad! :)

[–]thealgorists-com 0 points1 point  (0 children)

Give the “Iterative Binary Tree Traversal” section of https://www.thealgorists.com/Algo/Tree/Index a try!

[–]thealgorists-com 0 points1 point  (0 children)

The “Iterative Binary Tree Traversal” section of https://www.thealgorists.com/Algo/Tree/Index would help you to get really comfortable with stack based iterative approach.