뒤에 두 갈래 나무가 두루 다니다

2704 단어
/**
 * 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 postorderTraversal(TreeNode* root) {
      vector vec;
      stack s;
      TreeNode* pCurrent = root;
      if(!root){
          return vec;
      }
       unordered_map map;
     while(!s.empty() || pCurrent){
        if(pCurrent && !map[pCurrent]){
            s.push(pCurrent);
            map[pCurrent] = true;
            pCurrent = pCurrent->left;
        }else{ 
            TreeNode *pNode = s.top();
            pCurrent = pNode->right;
            if(!pCurrent || map[pCurrent]){
                vec.push_back(pNode->val);
                s.pop();
                pCurrent = nullptr;
            } 
        }
    }
      return vec;
    }
      
     /*vector postorderTraversal(TreeNode* root) {
      vector vec;
      stack s;
      unordered_map map;
      if(!root){
          return vec;
      }
      s.push(root);
      while(!s.empty()){
        TreeNode* node = s.top();
        if(node->left && !map[node->left]){
            s.push(node->left);
            map[node->left] = true;
        }else{
          if(node->right && !map[node->right]){
            s.push(node->right);       
            map[node->right] = true;
          }else{
            s.pop();
            vec.push_back(node->val);
          }
        }
      }
      return vec;
     }*/
    /*
       
      vector postorderTraversal(TreeNode* root) {
      vector vec;
      stack s;
      if(!root){
          return vec;
      }
      s.push(root);
      while(!s.empty()){
        TreeNode* node = s.top();
        if(node->left){
            s.push(node->left);
            node->left = NULL;
        }else{
          if(node->right){
            s.push(node->right);       
            node->right = NULL;
          }else{
            s.pop();
            cout<< node->val <val);
          }
        }
      }
      return vec;
    }*/
    
    /* 
     vector traversal(TreeNode* root, vector& vec){
        if(!root){
            return vec;
        }
        traversal(root->left, vec);
        traversal(root->right, vec);
        vec.push_back(root->val);
        return vec;
    }*/
    
    
};

좋은 웹페이지 즐겨찾기