[Python 3] BFS | Union-Find
\nclass Solution:\n    def minimumOperations(self, root: Optional[TreeNode]) -> int:\n        par = {}\n        rank = {}\n        \n        def ins(n1):\n     
```\nclass Solution:\n    def minimumOperations(self, root: Optional[TreeNode]) -> int:\n        par = {}\n        rank = {}\n        \n        def ins(n1):\n            if n1 in par:\n                return\n            par[n1] = n1\n            rank[n1] = 1\n        \n        def find(n1):\n            while n1 != par[n1]:\n                par[n1] = par[par[n1]]\n                n1 = par[n1]\n            return n1\n        \n        def union(n1, n2):\n            p1, p2 = find(n1), find(n2)\n            if p1 == p2:\n                return 0\n            if rank[p1] < rank[p2]:\n                p1, p2 = p2, p1\n            par[p2] = p1\n            rank[p1] += rank[p2]\n            return 1\n        \n        def helper(nums):\n            t = sorted(nums)\n            \n            for i, j in zip(nums, t):\n                ins(i)\n                ins(j)\n                union(i, j)\n            \n            return sum(rank[i] - 1 for i in nums if i == find(i))\n            \n        \n        q = deque( [root] )\n        res = 0\n        \n        while q:\n            level = []\n            \n            for _ in range(len(q)):\n                cur = q.popleft()\n                level.append(cur.val)\n                \n                if cur.left: q.append(cur.left)\n                if cur.right: q.append(cur.right)\n                    \n            res += helper(level)\n        \n        return res\n```
['Breadth-First Search', 'Union Find', 'Python', 'Python3']