/**
 * struct TreeNode {
 *	int val;
 *	struct TreeNode *left;
 *	struct TreeNode *right;
 *	TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 * };
 */
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param root TreeNode类 
     * @return int整型vector
     */
    void preorder(vector<int> &res,TreeNode* root)
    {
        //遇到空节点则返回
        if(!root)
            return;
        //先遍历根节点
        res.push_back(root->val);
        //再去左子树
        preorder(res, root->left);
        //最后去右子树
        preorder(res, root->right);
    }
    vector<int> preorderTraversal(TreeNode* root) {
        // write code here
        vector<int> res;
        preorder(res, root);
        return res;
    }
};