LeetCode 145. Binary Tree Postorder Traversal

LeetCode 145

class Solution {
public:
    TreeNode* s[100005];
    vector<int> ans;
    int tag=0;
    vector<int> postorderTraversal(TreeNode* root) {
        
         if(root==NULL) return ans;
        s[tag++]=root;
        TreeNode* term;
        while(tag>0)
        {   
             term = s[tag-1];
           if(term->left!=NULL&&term->left->val!=-999999)
           {s[tag++]=term->left;continue;}
                     
           if(term->right!=NULL&&term->right->val!=-999999)
           {s[tag++]=term->right;continue;}
           
            if(s[tag-1]->val!=-999999)
            ans.push_back(s[tag-1]->val);
           (*s[tag-1]).val=-999999;
           tag--;
           
        } 
        return ans;
        
    }
};

和上一题几乎一样

猜你喜欢

转载自www.cnblogs.com/dacc123/p/10025575.html