# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param root TreeNode类 
# @return int整型一维数组
#
class Solution:
    def preorderTraversal(self, root: TreeNode) -> List[int]:  # 迭代法1
        # 根结点为空则返回空列表
        if not root:
            return []
        stack = [root]
        result = []
        while stack:
            node = stack.pop()
            # 中结点先处理
            result.append(node.val)
            # 右孩子先入栈
            if node.right:
                stack.append(node.right)
            # 左孩子后入栈
            if node.left:
                stack.append(node.left)
        return result

class Solution:
    def preorderTraversal(self, root: TreeNode) -> List[int]:  # 迭代法2
        result = []
        st= []
        if root:
            st.append(root)
        while st:
            node = st.pop()
            if node != None:
                if node.right: #右
                    st.append(node.right)
                if node.left: #左
                    st.append(node.left)
                st.append(node) #中
                st.append(None)
            else:
                node = st.pop()
                result.append(node.val)
        return result

class Solution:
    def preorderTraversal(self , root: TreeNode) -> List[int]:  # 递归法
        # write code here
        result = list()
        def traversal(root):
            if root == None:
                return None
            result.append(root.val)
            traversal(root.left)
            traversal(root.right)
        traversal(root)
        return result