Why RE? Who can help me?


  • 0
    Z
    class Solution {
    public:
        vector<vector<int>> levelOrder(TreeNode* root) 
        {
            vector<vector<int>> ans;
            int i = 0;
            if(root == NULL)
                return ans;
            queue<TreeNode *> que;
            que.push(root);
            while(!que.empty())
            {
                for(int j = 0, len = que.size(); j < len; j++)
                {
                    TreeNode * temp = que.front();
                    ans[i].push_back(temp->val);
                    que.pop();
                    if(!temp->left)     que.push(temp->left);
                    if(!temp->right)    que.push(temp->right);
                }
                i++;
            }
            return ans;
        }
    };

  • 0
    J
                ans[i].push_back(temp->val);
    

    maybe there is something wrong with it, because you didn't define the length of inside the ans.

    I am not sure if what I said, I just guess it.
    I am not familiar with C++, I use java


  • 0
    Z

    you are right. I always forgot this... ans[i].push_back() memory overflow. I rewrite my code and now whatever i input, it always just output the root node. Something is wrong with my algorithm. Thank you all the same.


Log in to reply
 

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