前:
中:
后:
//前序遍历
class Solution {
public:
void funcOfRecursion(TreeNode* cur, vector<int>& vec) {
if (cur == nullptr) return;
vec.emplace_back(cur->val);
funcOfRecursion(cur->left,vec);
funcOfRecursion(cur->right,vec);
}
vector<int> preorderTraversal(TreeNode* root) {
vector<int> vec = {};
if (root == nullptr) return vec;
funcOfRecursion(root,vec);
return vec;
}
};
//中序遍历
class Solution {
public:
void funcOfRecursion(TreeNode* cur,vector<int>& vec) {
if(cur == nullptr) return;
funcOfRecursion(cur->left,vec);
vec.emplace_back(cur->val);
funcOfRecursion(cur->right,vec);
}
vector<int> inorderTraversal(TreeNode* root) {
vector<int> vec = {};
if(root == nullptr) return vec;
funcOfRecursion(root,vec);
return vec;
}
};
//后序遍历
class Solution {
public:
void funcOfRecursion(TreeNode* cur, vector<int>& vec) {
if(cur == nullptr) return;
funcOfRecursion(cur->left,vec);
funcOfRecursion(cur->right,vec);
vec.emplace_back(cur->val);
}
vector<int> postorderTraversal(TreeNode* root) {
vector<int> vec = {};
if(root == nullptr) return vec;
funcOfRecursion(root,vec);
return vec;
}
};