Leetcoder 前序,中序,后序遍历代码
class Solution { public: vector<int> postorderTraversal(TreeNode* root) { vector<int> vec; travel(root, vec); return vec; } //后序遍历 void travel(TreeNode* root, vector<int>& v) { if (root == NULL)return; travel(root->left,v); travel(root->right, v); v.push_back(root->val); } //前序遍历 void pretravel(TreeNode* root, vector<int>& v) { if (root == NULL)return; travel(root->left,v); travel(root->right, v); v.push_back(root->val); } //中序遍历 void midtravel(TreeNode* root, vector<int>& v) { if (root == NULL)return; travel(root->left,v); v.push_back(root->val); travel(root->right, v); } };