knakul853

Untitled

Jun 10th, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. /**
  2.  knakul853
  3.  */
  4. class Solution {
  5. public:
  6.     vector<int>ans;
  7.     vector<int> postorderTraversal(TreeNode* root) {
  8.        
  9.         vector<int>ans;
  10.         stack<TreeNode*>st;
  11.         while(!st.empty() || root )
  12.         {
  13.             if( root )
  14.             {
  15.                 st.push(root);
  16.                 ans.push_back(root->val);
  17.                 root = root->right;
  18.             }
  19.            
  20.             else{
  21.                 root = st.top();st.pop();
  22.                 root = root->left;
  23.             }
  24.            
  25.         }
  26.        
  27.        reverse(ans.begin(), ans.end());
  28.         return ans;
  29.     }
  30. };
  31. /*
  32.  
  33. class Solution {
  34. public:
  35.     vector<int>ans;
  36.     vector<int> postorderTraversal(TreeNode* root) {
  37.        
  38.         vector<int>ans;
  39.         stack<TreeNode*>st;
  40.         while(!st.empty() || root )
  41.         {
  42.             if( root )
  43.             {
  44.                 st.push(root);
  45.                 ans.push_back(root->val);
  46.                 root = root->right;
  47.             }
  48.            
  49.             else{
  50.                 root = st.top();st.pop();
  51.                 root = root->left;
  52.             }
  53.            
  54.         }
  55.        
  56.        reverse(ans.begin(), ans.end());
  57.         return ans;
  58.     }
  59. };
  60.  
  61. */
Add Comment
Please, Sign In to add comment