class Solution(object):

    def preorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        res = []
        stack = []
        temp = root
        while temp or stack:
            if temp:
                res.append(temp.val)
                stack.append(temp)
                temp = temp.left
            else:
                temp = stack.pop()
                temp = temp.right
        return res

2d37073f0bbd8c795eb212366ecb83726f30ea7a
<mask token>

class Solution(object):
    <mask token>

class Solution(object):

    def preorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        res = []
        stack = []
        temp = root
        while temp or stack:
            if temp:
                res.append(temp.val)
                stack.append(temp)
                temp = temp.left
            else:
                temp = stack.pop()
                temp = temp.right
        return res
