```
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> v;
if(root == NULL) {
return v;
}
stack<TreeNode*> s;
s.push(root);
TreeNode* node;
while(!s.empty()) {
node = s.top();
if(node->left == NULL && node->right == NULL) {
v.push_back(node->val);
s.pop();
} else {
if(node->right != NULL) s.push(node->right);
if(node->left != NULL) s.push(node->left);
node->right = NULL;
node->left = NULL;
}
}
return v;
}
};
```