Why I am getting Memory Limit Exceeded...any help?

Tags: #<Tag:0x00007f2422222390>


vector<vector<int> > Solution::levelOrder(TreeNode* root) { if(root == NULL) return {}; queue<TreeNode*> q; TreeNode* temp = NULL; vector<vector<int>> ans; q.push(root); q.push(NULL); vector<int> t; while(!q.empty()){ TreeNode *curr = q.front(); q.pop(); if(curr == NULL){ ans.push_back(t); t.clear(); q.push(NULL); }else{ t.push_back(curr->val); if(curr->left != NULL) q.push(curr->left); if(curr->right != NULL) q.push(curr->right); } } return ans; }


when curr is null , you should check if the queue is empty , if not than only should you push a null so just check if (!q.empty()) than do q.push(null) otherwise your queue is never empty as you always insert a null


run while loop if q.size()>1
as u insert the null after every level and queue never gets end