```vector<int> c,p,m; 本文思路倒是不难,我在遍历时犯了一个低级错误.前序遍历,中序,后序遍历本该是全局变量的参数,我给写到函数内部变成局部变量了。 return局部变量会导致函数完成后,所指向的内存空间也随之被释放掉,随之报错 /** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * * @param root TreeNode类 the root of binary tree * @return int整型vector<vector<>> */ vector<int> c,p,m; //return局部变量会导致函数完成后,所指向的内存空间也随之被释放掉 //前序遍历 vector<int> BinaryTreeP(TreeNode* root) { if(root==nullptr) { return p; } p.push_back(root->val); BinaryTreeP(root->left); BinaryTreeP(root->right); return p; } //中序遍历 vector<int> BinaryTreeC(TreeNode* root) { if(root==nullptr) { return c; } BinaryTreeC(root->left); c.push_back(root->val); BinaryTreeC(root->right); return c; } //后序遍历 vector<int> BinaryTreeH(TreeNode* root) { if(root==NULL) { return m; } BinaryTreeH(root->left); BinaryTreeH(root->right); m.push_back(root->val); return m; } vector<vector<int> > threeOrders(TreeNode* root) { // write code here vector<vector<int> > v; v.push_back(BinaryTreeP(root)); v.push_back(BinaryTreeC(root)); v.push_back(BinaryTreeH(root)); return v; } };