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)


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