前序遍历
class Solution {
public:
void dfs(TreeNode* node,vector<int>&s)
{
if(node==NULL) return;
s.push_back(node->val);
dfs(node->left,s);
dfs(node->right,s);
}
vector<int> preorderTraversal(TreeNode* root) {
vector<int>s;
dfs(root,s);
return s;
}
};
中序遍历
class Solution {
public:
void dfs(TreeNode* node,vector<int>&s)
{
if(node==NULL)
return;
dfs(node->left,s);
dfs(node->right,s);
s.push_back(node->val);
}
vector<int> postorderTraversal(TreeNode* root) {
vector<int>s;
dfs(root,s);
return s;
}
};
中序遍历
class Solution {
public:
void dfs(TreeNode* node,vector<int>& s)
{
if(node==NULL)
return ;
dfs(node->left,s);
s.push_back(node->val);
dfs(node->right,s);
}
vector<int> inorderTraversal(TreeNode* root) {
vector<int>s;
dfs(root,s);
return s;
}
};