Guest User

Untitled

a guest
Jan 21st, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. vector<int> postorderTraversal(TreeNode* root) {
  13. vector<int> res;
  14. stack<TreeNode*> st;
  15. if(root == NULL) return res;
  16. st.push(root);
  17. while(!st.empty()){
  18. TreeNode* temp = st.top();
  19. st.pop();
  20. res.push_back(temp->val);
  21. if(temp->left != NULL)
  22. st.push(temp->left);
  23. if(temp->right != NULL)
  24. st.push(temp->right);
  25. }
  26. reverse(res.begin(), res.end());
  27. return res;
  28. }
  29. };
Add Comment
Please, Sign In to add comment