一、144前序遍历:注意定义函数的返回值为void
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void preorderTraversal (TreeNode* cur, vector<int> &vec){//无返回值
if(cur==NULL) return;//NULL大写注意
vec.push_back(cur->val);//中
preorderTraversal(cur->left,vec);//左
preorderTraversal(cur->right,vec);//右
}
vector<int> preorderTraversal(TreeNode* root) {
vector<int> result;
preorderTraversal(root,result);
return result;
}
};