I have improve my previous method(BFS)


  • 1
    L

    class Solution {
    public:
    vector<vector<int>> levelOrder(TreeNode* root) {

        vector<TreeNode*> a;
        vector<int> b;
        vector<vector<int>> d;
        if(!root)
        return d;
        a.push_back(root);
        while(a.size()){
            int levelsize=a.size();
            b.clear();
            for(int i=0;i<levelsize;i++){
                TreeNode* qq=a.front();
                a.erase(a.begin());
                b.push_back(qq->val);
              if(qq->left) {
                a.push_back(qq->left);}
              if(qq->right) {
                a.push_back(qq->right);}
            }
          d.push_back(b);
       }
       return d; 
    }
    

    };


Log in to reply
 

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