Error: Memory Limit Exceeded, why?


  • 0
    F
    class Solution {
    vector<int> result;
    public:
    vector<int> postorderTraversal(TreeNode *root) {
        if(root == NULL)
            return result;
    
        stack<TreeNode*> s;
        s.push(root);
    
        TreeNode *tleft, *tright;
        tleft = root->left;
        TreeNode *t;
    
        while(!s.empty())
        {
            while(tleft != NULL)
            {
                s.push(tleft);
                tleft = tleft->left;
            }
    
            t = s.top();
            tright = t->right;
    
            if(tright != NULL)
            {
                s.push(tright);
                tleft = tright->left;
            }
            else
            {
                result.push_back(t->val);
                if(!s.empty())
                {
                    t = s.top();
                    s.pop();
                    result.push_back(t->val);
                 }
            }
    
        }
    
        return result;
    
    }};
    

    why my code Memory Limited Exceeded? how to improve? thanks!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.