2022.0806算法第9题二叉树的前序遍历
二叉树的前序遍历为根左右,这种结构适合递归调用。
递归函数为每次都需要做的步骤,也就是遍历根左右。
void preorder(vector<int> &res,TreeNode* root)
{
    if(root==NULL)
        return ;
    res.push_back(root->val);
    preorder(res, root->left);
    preorder(res, root->right);
}
然后在主程序进行调用函数即可
vector<int> ans;
preorder(ans, root);
return ans;
递归代码简单,但是感觉自己想的话还是挺难的