Given a binary tree, return the postorder traversal of its nodes' values.
Example:
Input:[1,null,2,3]
1 \ 2 / 3 Output:[3,2,1]
题目链接:https://leetcode.com/problems/binary-tree-postorder-traversal/
/**
* 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:
void post_order(TreeNode* root,vector<int>&res)
{
if(root)
{
post_order(root->left,res);
post_order(root->right,res);
res.emplace_back(root->val);
}
}
vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
post_order(root,res);
return res;
}
};