C++ || Unordered map || Easy approach
Here is my c++ code for this problem.\n\'\'\'\n\n\t/\n\t * Definition for a binary tree node.\n\t * struct TreeNode {\n\t *     int val;\n\t *     TreeNode left
Here is my c++ code for this problem.\n\'\'\'\n\n\t/**\n\t * Definition for a binary tree node.\n\t * struct TreeNode {\n\t *     int val;\n\t *     TreeNode *left;\n\t *     TreeNode *right;\n\t *     TreeNode() : val(0), left(nullptr), right(nullptr) {}\n\t *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n\t *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n\t * };\n\t */\n\tclass Solution {\n\tpublic:\n\t\tint minimumOperations(TreeNode* root) {\n\t\t\tqueue<TreeNode*>q;\n\t\t\tq.push(root);\n\t\t\tvector<vector<int>>v;\n\t\t\twhile(!q.empty()){\n\t\t\t\tint l=q.size();\n\t\t\t\tvector<int>temp;\n\t\t\t\tfor(int i=0; i<l; i++){\n\t\t\t\t\troot=q.front();\n\t\t\t\t\ttemp.push_back(root->val);\n\t\t\t\t\tq.pop();\n\t\t\t\t\tif(root->left){q.push(root->left);}\n\t\t\t\t\tif(root->right){q.push(root->right);}\n\t\t\t\t}\n\t\t\t\tv.push_back(temp);\n\t\t\t}\n\t\t\t//for(int i=0; i<v.size(); i++){for(int j=0; j<v[i].size(); j++){cout<<v[i][j]<<" ";}}cout<<endl;\n\t\t\tlong long int cnt=0;\n\t\t\tfor(int i=0; i<v.size(); i++){\n\t\t\t\t//unordered_map<int, int>m;\n\t\t\t\tunordered_map<int, int>m1;\n\t\t\t\tvector<pair<int, int>>x;\n\t\t\t\tfor(int j=0; j<v[i].size(); j++){\n\t\t\t\t\tx.push_back({v[i][j], j});\n\t\t\t\t\t//m[v[i][j]]=j;\n\t\t\t\t}\n\t\t\t\tsort(x.begin(), x.end());\n\t\t\t\tfor(int k=0; k<x.size(); k++){\n\t\t\t\t\t//cout<<x[k].second<<" "<<k<<endl;\n\t\t\t\t\tm1[x[k].second]=k;\n\t\t\t\t}\n\t\t\t\t// for(int k=0; k<x.size(); k++){\n\t\t\t\t//     cout<<x[k].first<<" "<<x[k].second<<" "<<m1[x[k].second]<<endl;\n\t\t\t\t// }cout<<endl;\n\t\t\t\tfor(int k=0; k<x.size(); k++){\n\t\t\t\t\t// cout<<endl;\n\t\t\t\t\t// cout<<x[k].first<<" "<<x[k].second<<" "<<cnt<<" "<<k<<endl;\n\t\t\t\t\t// cout<<endl;\n\t\t\t\t\tif(x[k].second==k){continue;}\n\t\t\t\t\telse{\n\n\t\t\t\t\t\tcnt++;\n\t\t\t\t\t\tint l=m1[x[k].second], r=m1[m1[x[k].second]], f2=x[r].second, f1=x[k].second, v1=m1[f1], v2=m1[f2], ind=x[k].second;\n\t\t\t\t\t\t//cout<<l<<" "<<r<<" "<<f1<<" "<<f2<<" "<<v1<<" "<<v2<<endl;\n\t\t\t\t\t\tx[r].second=f1;\n\t\t\t\t\t\tx[l].second=f2;\n\t\t\t\t\t\tm1[f1]=v2;\n\t\t\t\t\t\tm1[f2]=v1;\n\t\t\t\t\t\t//cout<<x[k].first<<" "<<x[k].second<<" "<<cnt<<" "<<k<<endl;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t// for(int k=0; k<x.size(); k++){\n\t\t\t\t//     cout<<x[k].first<<" "<<x[k].second<<" "<<m1[x[k].second]<<endl;\n\t\t\t\t// }cout<<endl;\n\t\t\t}\n\t\t\treturn cnt;\n\t\t}\n\t};\n\'\'\'
['C']