Solving Tree Problems on LeetCode – Algorithms and Leetcode – Medium
For this way, because traverse is a DFS method, which makes us think about using stack to save the result. Note: because the stack is FILO, if we want to visit left subtree at first, we need to push the right subtree at first. This iterative implementation is better than the recursive version, because the memory we use here is the heap memory = memory size. While for the recursive version, it uses the stack memory = processing memory, so it is easier to run out of memory.
Read full article from Solving Tree Problems on LeetCode – Algorithms and Leetcode – Medium
No comments:
Post a Comment