和先序遍历类似,根节点的操作放在最后间就可。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> path;
vector<int> postorderTraversal(TreeNode* root) {
if(root!=NULL){
postorderTraversal(root->left);
postorderTraversal(root->right);
path.push_back(root->val);
}
return path;
}
};