/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* reverseOddLevels(TreeNode* root) {
queue<TreeNode*> qu;
vector<TreeNode*> t;
qu.push(root);
int level = 0;
while(!qu.empty()) {
int n = qu.size();
for(int i = 0; i < n; i++) {
TreeNode* p = qu.front();
qu.pop();
if(level) {
t.push_back(p);
}
if(p->left != nullptr) {
qu.push(p->left);
qu.push(p->right);
}
}
level ^= 1;
int len = t.size();
int l = 0, r = len - 1;
while(l < r) {
swap((t[l])->val, (t[r])->val);
l++;
r--;
}
t.clear();
}
return root;
}
};
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!