Esay Solution explanation

programming
Tags: #<Tag:0x00007f182c98a968>

#1
   int Solution::solve(TreeNode* root) {
         int msum = 0, lvl = 0, msum_lvl = 0;
     vector<TreeNode*> q{root};
     
     while (!q.empty()) {
         vector<TreeNode*> q1;
         int sum = 0;
         ++lvl;
         for (auto n : q) {
             sum += n->val;
             if (n->left != nullptr) 
                 q1.push_back(n->left);
             if (n->right != nullptr) 
                 q1.push_back(n->right);
         }
         msum_lvl = sum < msum ? msum_lvl : lvl;
         msum = max(sum, msum);
         swap(q, q1);
     }
       
       return msum;
   }