Python solution


  • 0
    C

    solution 1:

    class Solution(object):
        def preorderTraversal(self, root):
            """
            :type root: TreeNode
            :rtype: List[int]
            """
            if root==None:
                return []
    
            stack = []
            result = []
            while root or stack:
                while root:
                    result.append(root.val)
                    stack.append(root)
                    root = root.left
                if stack:
                    root = stack.pop()
                    root = root.right
            return result

Log in to reply
 

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