-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
37 lines (31 loc) · 890 Bytes
/
solution.py
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
26
27
28
29
30
31
32
33
34
35
36
37
# Definition for a binary tree node.
from typing import List, Optional
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
# solution 1:
# res = []
# def inorder(root):
# if not root:
# return
# inorder(root.left)
# res.append(root.val)
# inorder(root.right)
# inorder(root)
# return res
# solution 2:
res = []
stack = []
curr = root
while curr or stack:
while curr:
stack.append(curr)
curr = curr.left
curr = stack.pop()
res.append(curr.val)
curr = curr.right
return res