Runtime Error when input is [1] ,who can help here?


  • 0
    Y
    class Solution {
    public:
        vector<vector<int> > levelOrder(TreeNode *root) {
        vector<vector<int> > vec;
        vector<TreeNode *> s1,s2;
        if(!root) return vec;
        s1.push_back(root);
        TreeNode *ptr;
        do{ vector<int> tmp;
            for(int i=0;i<s1.size();i++){
                tmp.push_back(s1[i]->val);
                if(!s1[i]->left)  s2.push_back(s1[i]->left);
                if(!s1[i]->right) s2.push_back(s1[i]->right);
            }
            if(!tmp.empty()) vec.push_back(tmp);
            s1.clear();tmp.clear();
            for(int i=0;i<s2.size();i++){
                tmp.push_back(s2[i]->val);
                if(!s2[i]->left)  s1.push_back(s2[i]->left);
                if(!s2[i]->right) s1.push_back(s2[i]->right); 
            }
            if(!tmp.empty()) vec.push_back(tmp);
            tmp.clear();s2.clear();
        }while(s2.size()!=0||s1.size()!=0);
        return vec;
    }
    };

Log in to reply
 

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