C++ simple solution
Intuition\n Describe your first thoughts on how to solve this problem. \nIf we are able to get number of swaps required for elements in each level, we can solve
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIf we are able to get number of swaps required for elements in each level, we can solve it with bfs\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nGet elements of each element in array. \nsort the array, put value and index of the sorted value in a map.\nFor each element in array, check if it\'s in the correct place, if not swap with correct index value, increase count.\nreturn count\n\n# Complexity\n- Time complexity: O(nlogn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode() : val(0), left(nullptr), right(nullptr) {}\n *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n    int minSwaps(vector<int> arr) {\n        vector<int> copy = arr;\n        sort(copy.begin(), copy.end());\n        unordered_map<int, int> mp;\n        for (int i = 0; i < arr.size(); i++) mp[copy[i]] = i; \n        int count = 0;\n        for (int i = 0; i < arr.size(); i++) {\n            while (mp[arr[i]] != i) {\n                swap(arr[i], arr[mp[arr[i]]]);\n                count++;\n            }\n        }\n        return count;\n    }\n\n    vector<int> getArray(vector<TreeNode*> arr) {\n        vector<int> res;\n        for (int i = 0; i < arr.size(); i++) {\n            res.push_back(arr[i] -> val);\n        }\n        return res;\n    }\n    int minimumOperations(TreeNode* root) {\n        vector<TreeNode*> currentLevel, nextLevel;\n        nextLevel.push_back(root);\n        int count = 0;\n        while (nextLevel.size() > 0) {\n            swap(currentLevel, nextLevel);\n            nextLevel.clear();\n\n            count += minSwaps(getArray(currentLevel));\n            for (int i = 0; i < currentLevel.size(); i++) {\n                if (currentLevel[i]->left) nextLevel.push_back(currentLevel[i]->left);\n                if (currentLevel[i]->right) nextLevel.push_back(currentLevel[i]->right);\n            }\n        }\n        return count;\n    }\n};\n```
['C++']