count swaps on each level ->bfs
\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode() : val(0)
```\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    \n    int cnt(vector<int> &v)\n    {\n        vector<int> sv(v.begin(),v.end());\n        sort(sv.begin(),sv.end());\n        unordered_map<int,int> mp;\n        int ans = 0;\n        int g = 0;\n        for(auto i: v)\n        {\n            mp[i] = g++; \n        }\n        \n        int n = v.size();\n        for(int i=0;i<n;i++)\n        {\n            if(v[i] == sv[i])\n                continue;\n            int c = mp[sv[i]];\n            mp[v[i]] = c;\n            mp[v[c]] = i;\n            swap(v[c],v[i]);\n            ans++;\n        }\n        \n        return ans;\n        \n    }\n    \n    int bfs(TreeNode* r)\n    {\n        int res = 0;\n        queue<TreeNode*> q;\n        q.push(r);\n        while(!q.empty())\n        {\n            int sz = q.size();\n            vector<int> temp;\n            while(sz--)\n            {\n                auto v = q.front();\n                q.pop();\n                temp.push_back(v->val);\n                if(v->left)\n                    q.push(v->left);\n                if(v->right)\n                    q.push(v->right);\n            }\n            \n            res += cnt(temp);\n        }\n        \n        return res;\n    }\n    \n    int minimumOperations(TreeNode* r) {\n        \n        return bfs(r);\n        \n        \n        \n    }\n};\n```
['C']