Simply Python Iterative Solution

  • 0
        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)
            return lst

Log in to reply

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