理解前中后序遍历的链接
https://mp.weixin.qq.com/s/PwVIfxDlT3kRgMASWAMGhA
class Solution {
vector<int> a;//创造一个向量放遍历的值
public:
void transfer(TreeNode* root)//递归第一要素:递归函数的参数以及返回值
{
if(root==nullptr)
return;
//labuladong算法小抄里说的模板就是下面三行的位置,前序遍历:中左右;中序遍历:左中右;后序遍历:左右中。
a.push_back(root->val);//中
transfer(root->left);//左
transfer(root->right);//右
}
vector<int> preorderTraversal(TreeNode* root) {
transfer(root);
return a;
}
};