text: # Definition for a binary tree node.
  # class TreeNode(object):
  #     def __init__(self, x):
  #         self.val = x
  #         self.left = None
  #         self.right = None
  
  class Solution(object):
      def preorderTraversal(self, root):
          result = []
          if not root:
              return result
          stack = [root]
          while stack:
              curr = stack.pop()
              result.append(curr.val)
              if curr.right:
                  stack.append(curr.right)
              if curr.left:
                  stack.append(curr.left)
          return result
  