goodd one
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n#include <iostream>\n#include <vector>\n#include <queue>\n\nclass Solution \n{\nprivate:\n    struct TreeNode\n    {\n        int val;\n        TreeNode* left;\n        TreeNode* right;\n        TreeNode* parent;\n\n        TreeNode(const int& v)\n        {\n            val = v;\n            left = nullptr;\n            right = nullptr;\n            parent = nullptr;\n        }\n    };\n\n    class Tree\n    {\n    public:\n        Tree(const std::vector<int>& v)\n        {\n            root = buildTreeFrom(v, 0, v.size() - 1);\n        }\n\n        TreeNode* find(const int& val)\n        {\n            return findNode(root, val);\n        }\n\n        TreeNode* next(TreeNode* node)\n        {\n            if (node->right)\n            {\n                node = node->right;\n                while (node->left)\n                    node = node->left;\n\n                return node;\n            }\n        \n            TreeNode* parent = node->parent;\n            while (parent && node == parent->right) \n            {\n                node = parent;\n                parent = parent->parent;\n            }\n            return parent;\n        }\n\n        TreeNode* erase(TreeNode* node)\n        {\n            if (!node)\n                return nullptr;\n\n            TreeNode* parent = node->parent;\n            if (!node->left && !node->right)\n            {\n                TreeNode* nextNode = next(node);\n                if (!parent)\n                {\n                    root = nullptr;\n                }\n                else\n                {\n                    if (parent->left == node)\n                        parent->left = nullptr;\n                    else\n                        parent->right = nullptr;\n                }\n                \n                return nextNode;\n            }\n            else if (!node->left) \n            {\n                TreeNode* nextNode = next(node);\n\n                if (!parent)\n                {\n                    root = node->right;\n                    node->right->parent = nullptr;\n                }\n                else if (parent->left == node)\n                {\n                    parent->left = node->right;\n                    node->right->parent = parent;\n                }\n                else\n                {\n                    parent->right = node->right;\n                    node->right->parent = parent;\n                }\n\n                delete node;\n                return nextNode;\n            }\n            else if (!node->right) \n            {\n                TreeNode* nextNode = next(node);\n                if (!parent)\n                {\n                    root = node->left;\n                    node->left->parent = nullptr;\n                }\n                else if (parent->left == node)\n                {\n                    parent->left = node->left;\n                    node->left->parent = parent;\n                }\n                else\n                {\n                    parent->right = node->left;\n                    node->left->parent = parent;\n                }\n\n                delete node;\n                return nextNode;\n            }\n            else\n            {\n                TreeNode* temp = node->right;\n                while (temp->left)\n                    temp = temp->left;\n\n                node->val = temp->val;\n\n                erase(temp);\n                return node;\n            }\n        }\n\n    private:      \n        TreeNode* buildTreeFrom(const std::vector<int>& vec, int start, int end)\n        {       \n            if (start > end)\n                return nullptr;\n        \n            int mid = (start + end) / 2;\n            TreeNode* node = new TreeNode(vec[mid]);\n            node->left  = buildTreeFrom(vec, start, mid - 1);\n            node->right = buildTreeFrom(vec, mid + 1, end);\n\n            if (node->left)\n                node->left->parent = node;\n            if (node->right)\n                node->right->parent = node;\n\n            return node;\n        }\n\n        TreeNode* findNode(TreeNode* node, const int& val)\n        {\n            TreeNode* stored = nullptr;\n            while (node)\n            {\n                if (node->val >= val)\n                {\n                    stored = node;\n                    node = node->left;\n                }\n                else\n                {\n                    node = node->right;\n                }\n            }\n            return stored;\n        }\n\n    private:\n        TreeNode* root;\n    };\n\npublic:    \n    std::vector<int> minReverseOperations(int n, int p, std::vector<int>& banned, int k) \n    {\n        std::vector<int> visited(n, -1);\n        std::vector<int> ban(n , 0);\n        for (const int& b: banned)\n        {\n            ban[b] = -1;\n            visited[b] = -1;\n        }\n        \n        std::vector<int> vOdd, vEven;\n        for (int i = 0; i < n; ++i) \n        {            \n            if (!ban[i] && i != p) \n            {\n                if (i & 1)\n                    vOdd.push_back(i);\n                else\n                    vEven.push_back(i);\n            }\n        }          \n\n        Tree odd(vOdd);\n        Tree even(vEven);    \n        \n        std::queue<int> q;\n        q.push(p);\n        visited[p] = 0;\n        while (!q.empty())    \n        {\n            int curr = q.front();\n            q.pop();\n\n            int begin = std::abs(curr - k + 1);\n            int end   = n - 1 - std::abs(n - curr - k);\n\n            Tree& tree = begin & 1 ? odd : even;\n            TreeNode* entry = tree.find(begin);\n            while (entry && entry->val <= end)\n            {\n                q.push(entry->val);\n                visited[entry->val] = visited[curr] + 1;\n                entry = tree.erase(entry);\n            }\n        }\n        return visited;\n    }\n};\n\n\n```
['C++']