# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None
classSolution(object): definorderTraversal(self, root): """ :type root: TreeNode :rtype: List[int] """ ans = [] l = [] l.append(root) while l: p = l.pop() # 向l中一直添加左子树 while p: l.append(p) p = p.left while l: p = l.pop() ans.append(p.val) if p.right: l.append(p.right) break return ans # def recuisive(root, ans): # if not root: # return # if root.left: # recuisive(root.left, ans) # ans.append(root.val) # if root.right: # recuisive(root.right, ans) # ans = [] # recuisive(root, ans) # return ans