level order + swap sort
class Solution:\n    def minimumOperations(self, root: Optional[TreeNode]) -> int:\n        \n        def swp(arr):\n            n = len(arr)\n            ans =
class Solution:\n    def minimumOperations(self, root: Optional[TreeNode]) -> int:\n        \n        def swp(arr):\n            n = len(arr)\n            ans = 0\n            temp = arr.copy()\n            h = {}\n            temp.sort()\n\n            for i in range(n):\n\n                h[arr[i]] = i\n\n            init = 0\n\n            for i in range(n):\n\n                if (arr[i] != temp[i]):\n                    ans += 1\n                    init = arr[i]\n                    arr[i], arr[h[temp[i]]] = arr[h[temp[i]]], arr[i]\n                    h[init] = h[temp[i]]\n                    h[temp[i]] = i\n\n            return ans\n        \n        count = 0\n        if not root:\n            return 0\n        queue = [root]\n        \n        while queue:\n            size = len(queue)\n            temp = []\n            for _ in range(size):\n                node = queue.pop(0)\n                temp.append(node.val)\n                if node.left:\n                    queue.append(node.left)\n                if node.right:\n                    queue.append(node.right)\n            count += swp(temp)\n        return count
['Python', 'Python3']