Created
January 21, 2019 01:11
-
-
Save zhangxiaomu01/e0dbced994eedc547506a6990784a51b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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