Easy Level order traversal with comments || [Python, Cpp]
\nclass Solution:\n    def cntSwap(self,arr):\n        \n        n = len(arr)\n        cnt = 0\n        a = sorted(arr)\n        index = {}\n        \n\t\t# sto
```\nclass Solution:\n    def cntSwap(self,arr):\n        \n        n = len(arr)\n        cnt = 0\n        a = sorted(arr)\n        index = {}\n        \n\t\t# store index of elements in arr\n        for ind in range(n):\n            index[arr[ind]] = ind\n\n        for ind in range(n):\n\t\t\t\n\t\t\t# If element in arr is out of position we need to swap hence increase cnt\n            if (arr[ind] != a[ind]):\n                cnt += 1\n                pos = arr[ind]\n                \n\t\t\t\t# Replace it with element that should be at this "ind" in arr using a[ind]\n                arr[ind], arr[index[a[ind]]] = arr[index[a[ind]]], arr[ind]\n\t\t\t\t\n\t\t\t\t# Update the indexes for swaped elements in "index"\n                index[pos] = index[a[ind]]\n                index[a[ind]] = ind\n\n        return cnt\n    \n    def minimumOperations(self, root: Optional[TreeNode]) -> int:\n        \n        res = 0\n        q = [root]\n        # Level order traversal\n        while q :\n            \n            level = []\n            values = []\n            for n in q:\n                \n                if n.left :\n                    level.append(n.left)\n                    values.append(n.left.val)\n                \n                if n.right :\n                    level.append(n.right)\n                    values.append(n.right.val)\n            \n            res += self.cntSwap(values)\n            q = level\n        \n        return res\n```\n\n```\nclass Solution {\nprivate:\n    int cntSwap(vector<int>& arr){\n        \n        int cnt = 0, size = arr.size();\n        vector<int> sortedArr = arr;\n        sort(sortedArr.begin(), sortedArr.end());\n        unordered_map<int, int> index;\n        \n\t\t// store index of elements in arr\n        for(int ind = 0; ind < size; ind++) index[arr[ind]] = ind;\n\n        for(int ind = 0; ind < size; ind++){\n\t\t\t\n\t\t\t// If element in arr is out of position we need to swap hence increase cnt\n            if(arr[ind] != sortedArr[ind]){\n                cnt += 1;\n                int pos = arr[ind];\n                \n\t\t\t\t// Replace it with element that should be at this "ind" in arr using a[ind]\n                swap(arr[ind], arr[index[sortedArr[ind]]]);\n\t\t\t\t\n\t\t\t\t// Update the indexes for swaped elements in "index"\n                swap(index[pos], index[sortedArr[ind]]);\n            }\n        }\n        return cnt;\n    }\n    \npublic:\n    int minimumOperations(TreeNode* root) {\n\n        if(!root) return 0;\n        vector<TreeNode*> q;\n        int res = 0;\n        q.push_back(root);\n\n        // Level order traversal\n        while(q.size()){\n            \n            vector<TreeNode*> level;\n            vector<int> values;\n\n            for(auto cur:q){\n\n                if(cur->left){\n                    level.push_back(cur->left);\n                    values.push_back(cur->left->val);\n                }\n                if(cur->right){\n                    level.push_back(cur->right);\n                    values.push_back(cur->right->val);\n                }\n            }\n            res += cntSwap(values);\n            q = level;\n        }\n        return res;\n    }\n};\n```
['Python', 'C++', 'Python3']