Skip to content

Instantly share code, notes, and snippets.

@zhangxiaomu01
Created January 21, 2019 01:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangxiaomu01/e0dbced994eedc547506a6990784a51b to your computer and use it in GitHub Desktop.
Save zhangxiaomu01/e0dbced994eedc547506a6990784a51b to your computer and use it in GitHub Desktop.
/**
* 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> postorderTraversal(TreeNode* root) {
vector<int> res;
stack<TreeNode*> st;
if(root == NULL) return res;
st.push(root);
while(!st.empty()){
TreeNode* temp = st.top();
st.pop();
res.push_back(temp->val);
if(temp->left != NULL)
st.push(temp->left);
if(temp->right != NULL)
st.push(temp->right);
}
reverse(res.begin(), res.end());
return res;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment