classSolution:defpreorderTraversal(self, root: Optional[TreeNode])-> List[int]:
ans =[]
stack = deque()
cur = root
if cur isNone:return ans
while stack or cur:while cur:
stack.append(cur.right)
ans.append(cur.val)
cur = cur.left
cur = stack.pop()return ans
classSolution:definorderTraversal(self, root: Optional[TreeNode])-> List[int]:
ans =[]
stack = deque()
cur = root
if cur isNone:return ans
while stack or cur:while cur:
stack.append(cur)
cur = cur.left
cur = stack.pop()
ans.append(cur.val)
cur = cur.right
return ans