File tree Expand file tree Collapse file tree 1 file changed +3
-3
lines changed Expand file tree Collapse file tree 1 file changed +3
-3
lines changed Original file line number Diff line number Diff line change 66- N = Number of elements in tree
77
88Make sure you understand the recursive solution first before attempting the iterative one.
9- The main point is to understand the recursive call and how it backtracks to the previous call when it reaches the base case.
9+ The main point is to understand is the recursive call and how it backtracks to the previous call when it reaches the base case.
1010
1111```
1212class Solution:
@@ -41,10 +41,10 @@ class Solution:
4141 stack, result = list(), list()
4242 curr_node = root
4343 while True:
44- if curr_node is not None:
44+ if curr_node is not None: # Going down the tree
4545 stack.append(curr_node)
4646 curr_node = curr_node.left
47- elif len(stack) > 0:
47+ elif len(stack) > 0: # Going up the tree (backtracking)
4848 curr_node = stack.pop()
4949 result.append(curr_node.val)
5050 curr_node = curr_node.right
You can’t perform that action at this time.
0 commit comments