Why my code got Memory Limit Exceeded?


  • 0
    L

    Hello,This is my code but got memory error. Can anyone help me to take a look at my code?

    class Solution {
    public:
        vector<vector<int> > levelOrder(TreeNode* root) {
          vector<int> v;
          vector<vector<int> > res;
          queue<TreeNode*> status;
    
          status.push(root);
          status.push(NULL);
          
          while(!status.empty()){
            while(status.front() != NULL){
              v.push_back(status.front()->val);
              if(status.front()->left != NULL) status.push(status.front()->left);
              if(status.front()->right != NULL) status.push(status.front()->right);
              status.pop();
            }
            res.push_back(v);
            v.resize(0);
            status.pop();
            if(!status.empty()) status.push(NULL);
          }
          return res;
        }
    };

Log in to reply
 

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