My C++ AC code which run all cases 7ms


  • 1
    L
    class Solution {
    public:
        vector<vector<int> > levelOrder(TreeNode *root) {
            
            vector<vector<int>> res;
            vector<TreeNode*> v;
        
            if (!root) return res;
        
            v.push_back(root);
        
            long cur = 0, last = 1;
        
            while(cur < v.size()){
                last = v.size();
                vector<int> temp;
                while ( cur < last){
                    temp.push_back(v[cur]->val);
                    if (v[cur]->left) v.push_back(v[cur]->left);
                    if (v[cur]->right) v.push_back(v[cur]->right);
                    cur ++;
                }
                res.push_back(temp);
            }
            return res;
        }
    };

Log in to reply
 

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