diff --git a/2415. Reverse Odd Levels of Binary Tree b/2415. Reverse Odd Levels of Binary Tree new file mode 100644 index 0000000..3d1ca07 --- /dev/null +++ b/2415. Reverse Odd Levels of Binary Tree @@ -0,0 +1,46 @@ +class Solution { +public: + TreeNode* reverseOddLevels(TreeNode* root) { + // we are given a perfect binary tree; + vector prev; + vector next; + queue q; + next.push_back(root); + q.push(root); + int lvl = 0; + while(next.empty() == false) { + // for(TreeNode* i : prev) cout<val<<" "; + // cout<val<<" "; + // cout<left = next[index]; + i->right = next[index + 1]; + index += 2; + } + + lvl+=1; + prev = next; + vector temp; + int q_size = q.size(); + while(q_size--) { + TreeNode* front = q.front(); + q.pop(); + if (front -> left != NULL) { + temp.push_back(front->left); + q.push(front->left); + } + if (front -> right != NULL) { + temp.push_back(front->right); + q.push(front->right); + } + } + next = temp; + } + return root; + } +};