1. #先序遍历
    2. class Solution:
    3. def inorderTraversal(self, root: TreeNode) -> List[int]:
    4. stack,res = [root],[]
    5. while(stack):
    6. h = stack.pop()
    7. if h == None:continue
    8. if isinstance(h,TreeNode):
    9. stack += [h.right,h.left,h.val]
    10. else:
    11. res.append(h)
    1. #中序遍历
    2. class Solution:
    3. def inorderTraversal(self, root: TreeNode) -> List[int]:
    4. stack,res = [root],[]
    5. while(stack):
    6. h = stack.pop()
    7. if h == None:continue
    8. if isinstance(h,TreeNode):
    9. stack += [h.right,h.val,h.left]
    10. else:
    11. res.append(h)
    12. return res
    1. #后序遍历
    2. class Solution:
    3. def inorderTraversal(self, root: TreeNode) -> List[int]:
    4. stack,res = [root],[]
    5. while(stack):
    6. h = stack.pop()
    7. if h == None:continue
    8. if isinstance(h,TreeNode):
    9. stack += [h.val,h.right,h.left]
    10. else:
    11. return res