C++✅✅ | Level Order Traversal + BFS + Hashing | Faster🧭 than 85%🔥 | Easy Understanding |
\n\n# Code\n\n\n\nclass Solution {\npublic:\n\nvoid swap(TreeNode* A, TreeNode* B)\n{\n    int tmp = A->val;\n    A->val = B->val;\n    B->val = tmp;\n}\n\n    
\n\n# Code\n```\n\n\nclass Solution {\npublic:\n\nvoid swap(TreeNode* A, TreeNode* B)\n{\n    int tmp = A->val;\n    A->val = B->val;\n    B->val = tmp;\n}\n\n    int minimumOperations(TreeNode* root) {\n  \n      queue<TreeNode*>q;\n      q.push(root);\n      int res = 0;\n\n      while(!q.empty())\n      {\n          int n = q.size();\n           vector<TreeNode*>v;\n\n          for(int i = 0; i < n ; ++i)\n          {\n              auto temp = q.front();\n              q.pop();\n\n              if(temp->left != NULL) q.push(temp->left);\n              if(temp->right != NULL) q.push(temp->right);\n\n              v.push_back(temp);\n          }\n\n          vector<int>copy;\n          unordered_map<int,int>mpp;\n\n          for(auto it : v) copy.push_back(it->val);\n          \n          \n          sort(copy.begin(), copy.end());\n\n          \n          for(int i = 0;i < copy.size(); ++i) mpp[copy[i]] = i;\n          \n         \n          for(int i = 0; i < v.size(); ++i)\n          {\n              if(mpp[v[i]->val] != i)\n              {\n                 swap(v[i], v[mpp[v[i]->val]]);\n                 res++;\n                 i--;\n              }\n          }\n\n      }\n\n      return res;  \n    }\n};\n\n\n```
['C++']