Simple java solution using BFS and Hashmap
\n\n# Code\n\nclass Solution {\n    public int minimumOperations(TreeNode root) {\n        Queue<TreeNode> nm=new LinkedList<>();\n        nm.offer(root);\n    
\n\n# Code\n```\nclass Solution {\n    public int minimumOperations(TreeNode root) {\n        Queue<TreeNode> nm=new LinkedList<>();\n        nm.offer(root);\n        int s=0;\n        while(!nm.isEmpty())\n        {\n            int m=0,l=nm.size();\n            List<Integer> kk=new ArrayList<>();\n            for(int i=0;i<l;i++)\n            {\n                if(nm.peek().left!=null)\n                {\n                    nm.add(nm.peek().left);\n                }\n                if(nm.peek().right!=null)\n                {\n                    nm.add(nm.peek().right);\n                }\n                int f=nm.poll().val;\n                kk.add(f);\n            }\n            s+=task(kk);\n        }\n        return s;\n    }\n    public int task(List<Integer> nm)\n    {\n        Map<Integer,Integer> kk=new HashMap<>();\n        for(int i=0;i<nm.size();i++)\n        {\n            kk.put(nm.get(i),i);\n        }\n        Collections.sort(nm);\n        boolean k[]=new boolean[nm.size()];\n        int s=0;\n        for(int i=0;i<nm.size();i++)\n        {\n            if(k[i] || kk.get(nm.get(i))==i)\n            {\n                continue;\n            }\n            int j=i,m=0;\n            while(!k[j])\n            {\n                k[j]=true;\n                j=kk.get(nm.get(j));\n                m++;\n            }\n            if(m>0)\n            {\n                s+=m-1;\n            }\n        }\n        return s;\n    }\n}\n```
['Java']