We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 57420fc commit f14ae04Copy full SHA for f14ae04
leetcode/medium/094_binary_tree_inorder_traversal.md
@@ -47,15 +47,13 @@ class Solution:
47
def inorderTraversal(self, root: TreeNode) -> List[int]:
48
stack, result = list(), list()
49
curr_node = root
50
- while True:
51
- if curr_node is not None: # Going down the tree
+ while stack or curr_node:
+ if curr_node:
52
stack.append(curr_node)
53
- curr_node = curr_node.left
54
- elif len(stack) > 0: # Going up the tree (backtracking)
55
- curr_node = stack.pop()
56
- result.append(curr_node.val)
57
- curr_node = curr_node.right
+ curr_node = curr_node.left # go left
58
else:
59
- break
+ curr_node = stack.pop() # go up
+ result.append(curr_node.val)
+ curr_node = curr_node.right # go right
60
return result
61
```
0 commit comments