题目:
给定一个二叉树,返回它的 后序 遍历。
题解思路:
方法一:递归+dfs
函数代码:
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int>res;
dfs(root,res);
return res;
}
void dfs(TreeNode *root,vector<int>&res)
{
if(!root)
{
return;
}
dfs(root->left,res);
dfs(root->right,res);
res.push_back(root->val);
}
};