-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path144.py
More file actions
25 lines (23 loc) · 714 Bytes
/
144.py
File metadata and controls
25 lines (23 loc) · 714 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def preorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
ans, stk = [], []
stk.append(root)
while stk:
p = stk.pop()
if p:
ans.append(p.val)
stk.append(p.right)
stk.append(p.left)
return ans
def preorderTraversal(self, root):
return [root.val] + self.preorderTraversal(root.left) + self.preorderTraversal(root.right) if root else []