leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

2415.cpp (649B)


      1 class Solution {
      2   public:
      3     TreeNode *reverseOddLevels(TreeNode *root) {
      4         static vector<TreeNode *> vec(8192);
      5         queue<TreeNode *> q({root});
      6         for (int lvl = 0; !q.empty(); lvl++) {
      7             for (int k = q.size() - 1; k >= 0; k--) {
      8                 vec[k] = q.front();
      9                 q.pop();
     10                 if (vec[k]->left) q.push(vec[k]->left);
     11                 if (vec[k]->right) q.push(vec[k]->right);
     12             }
     13             if (lvl % 2 == 0) continue;
     14             int i = 0, j = (1 << lvl) - 1;
     15             while (i < j)
     16                 swap(vec[i++]->val, vec[j--]->val);
     17         }
     18         return root;
     19     }
     20 };