[Python3] BFS
\n\nclass Solution:\n    def minimumOperations(self, root: Optional[TreeNode]) -> int:\n        ans = 0 \n        queue = deque([root])\n        while queue: \n
\n```\nclass Solution:\n    def minimumOperations(self, root: Optional[TreeNode]) -> int:\n        ans = 0 \n        queue = deque([root])\n        while queue: \n            vals = []\n            for _ in range(len(queue)): \n                node = queue.popleft()\n                vals.append(node.val)\n                if node.left: queue.append(node.left)\n                if node.right: queue.append(node.right)\n            mp = {x : i for i, x in enumerate(sorted(vals))}\n            visited = [0]*len(vals)\n            for i in range(len(vals)): \n                cnt = 0 \n                while not visited[i] and i != mp[vals[i]]: \n                    visited[i] = 1\n                    cnt += 1\n                    i = mp[vals[i]]\n                ans += max(0, cnt-1)\n        return ans \n```
['Python3']