```
vector<int> postorderTraversal(TreeNode *root) {
vector<int> a;
if(!root)
return a;
stack<TreeNode*> s;
s.push(root);
while(!s.empty()){
TreeNode *t = s.top();
if(t->right){
TreeNode *temp= t->right;
s.push(temp);
}
if(t->left){
TreeNode *temp = t->left;
s.push(temp);
}
if(!t->left && !t->right){
a.push_back(t->val);
s.pop();
}
t->right = NULL;
t->left = NULL;
}
return a;
}
```

May I know the solution that preserves the tree also. Thank you.