Why runtime error?


  • 0
    J

    My code is as below. I got:
    Submission Result: Runtime Error
    Last executed input: {1}
    Why is it? Thanks.

    class Solution {
    public:
        vector<vector<int> > levelOrderBottom(TreeNode *root) {
            vector<vector<int>> vec;
            if(!root) {
                return vec;
            }
            queue<TreeNode *> que;
            que.push(root);
            int index=0;
            while(!que.empty()) {
                size_t size = que.size();
                for(;size>0;size--) {
                    TreeNode *tmp = que.front();
                    que.pop();
                    vec[index].push_back(tmp->val);
                    if(tmp->left) {
                        que.push(tmp->left);
                    }
                    if(tmp->right) {
                        que.push(tmp->right);
                    }
                }
                index++;            
            }
            reverse(vec.begin(), vec.end());
            return vec;
        }
    };

  • 0
    S

    Before you execute vec(index).push back... you should create vec(index) first.


  • 0
    L

    The OJ expect to have an empty list, instead of a null result, if the input is a null tree.


Log in to reply
 

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