Skip to content

Commit 2dd06cd

Browse files
authored
Create 0144-binary-tree-preorder-traversal.py
1 parent 9d9dec7 commit 2dd06cd

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

Diff for: python/0144-binary-tree-preorder-traversal.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
3+
cur, stack = root, []
4+
res = []
5+
while cur or stack:
6+
if cur:
7+
res.append(cur.val)
8+
stack.append(cur.right)
9+
cur = cur.left
10+
else:
11+
cur = stack.pop()
12+
return res

0 commit comments

Comments
 (0)