# # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right
# # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right
classSolution: definorderTraversal(self, root: Optional[TreeNode]) -> List[int]: ans = [] st = [(root, False)] while st: thisNode, ifPushedChild = st.pop() ifnot thisNode: continue if ifPushedChild: ans.append(thisNode.val) else: st.append((thisNode.right, False)) st.append((thisNode, True)) st.append((thisNode.left, False)) return ans