Seems too complict, can be easier?


  • 0
    L

    class Solution {
    public:

    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<TreeNode*> a;
        vector<int> b,c;
        vector<vector<int>> d;
        if(!root)
        return d;
        int m=0,k=1,flag=0,j=0;
        a.push_back(root);
        c.push_back(k);
        while(a.size()){
            TreeNode* qq=a.front();
            a.erase(a.begin());
            j++;
            b.push_back(qq->val);
            flag=0;
            if(qq->left) {
                a.push_back(qq->left);
                flag++;}
            if(qq->right) {
                a.push_back(qq->right);
                flag++;}
                k+=flag;
                
            if(j==c[c.size()-1]){
              c.push_back(k);
              d.push_back(b);
              b.clear();
            }
    
       }
       return d; 
    }
    

    };


Log in to reply
 

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