Simply Python Iterative Solution


  • 0
    M
        def preorderTraversal(self, root):
            """
            :type root: TreeNode
            :rtype: List[int]
            """
            if not root:
                return []
            lst = []
            stack = [root]
            while stack:
                node = stack.pop()
                if node.right: stack.append(node.right)
                if node.left: stack.append(node.left)
                lst.append(node.val)
            return lst
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.