easy to understand short code using queue
Intuitionwe will add the values of nodes at a particlular level, sort them and then check how may are req to swap.Approachwe also create a visitied array and ch
# Intuition
we will add the values of nodes at a particlular level, sort them and then check how may are req to swap.
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
we also create a visitied array and check cycle size. there are always swaps = cycle size -1. we will use the logic and proceed
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity: nlogn
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: n
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int minimumOperations(TreeNode* root) {
        queue<TreeNode*> q;
        q.push(root);
        int ans = 0;
        while(!q.empty()){
            int n = q.size();
            vector<pair<int,int>> v(n);
            for(int i = 0; i<n; i++){
                TreeNode* node = q.front();
                q.pop();
                v[i] = {node->val , i};
                if(node->left) q.push(node->left);
                if(node->right) q.push(node->right);
            }
            sort(v.begin(), v.end());
            vector<bool> vis(n, false);
            for(int i =0; i<n; i++){
                if(vis[i] || v[i].second==i) continue;
                int cyc = 0;
                int x =i;
                while(!vis[x]){
                    vis[x] = true;
                    x= v[x].second;
                    cyc++;
                }
                if(cyc>1){
                    ans += cyc-1;
                }
            }
        }
        return ans;
    }
};
```
['Tree', 'Binary Tree', 'C++']