Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> res,right;
if(!root)
return {};
res=postorderTraversal(root->left);
right=postorderTraversal(root->right);
res.insert(res.end(),right.begin(),right.end());
res.push_back(root->val);
return res;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment