Divided into 2 sub problems || Explained approach 💯💯💯
IntuitionIt is combined question of "minimum swap requried to make array sorteed" --(search on web)-- and a normal BFS traversal (Level ordere traversal of tree
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
**It is combined question of "minimum swap requried to make array sorteed" --(search on web)-- and a normal BFS traversal (Level ordere traversal of tree)**
# Approach
<!-- Describe your approach to solving the problem. -->
1. First do the BFS traversal of tree
2. Initialize an empty array 
3. while doing traversal of one level of tree store every node of that in the array
4. after doing traversal of one level call a minswap function for every array.
5. in minswap function -- 
6. 1. first build a map which marks every value with its current index
   2. Initialize a visited vector of size equal to the unorted vector this will store which index is visited or not
   3. Now sort the given vector
   4. now traverse the sorted vector and if the index is not visited perform the following operations: 
       i. initialize a int value say cycle which will store the length of cycle
        ii. if cycle length is more than 1 than add cycle - 1 to ans
    5. return the ans value
7. add up all values of all level obatined from minswap function

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
        O(NlogN)
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
        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 {
    int minswap(vector<int> &v){
        int ans = 0;
        vector<int> vis(v.size(),0);
        map<int,int> mp;
        for(int i = 0;i<v.size();i++){
            mp[v[i]] = i;
        }
        sort(v.begin(),v.end());

        for(int i = 0;i<v.size();i++){

            if(vis[i] || mp[v[i]] == i) continue;

            int j = i,cycle = 0;
            while(!vis[j]){
                vis[j] = 1;
                cycle++;
                j = mp[v[j]];
            }
            if(cycle > 1){
                ans += cycle -1;
            }
        }
        return ans;
    }
public:
    int minimumOperations(TreeNode* root) {
        int ans = 0;
        queue<TreeNode*> q;
        q.push(root);
        while(!q.empty()){
            vector<int> v;
            int n = q.size();
            for(int i = 0;i<n;i++){
                TreeNode* temp = q.front();
                q.pop();
                v.push_back(temp -> val);
                if(temp -> left ) q.push(temp -> left);
                if(temp -> right) q.push(temp -> right);
            }
            ans += minswap(v);
        }
        return ans;
    }
};
```
**FOR MORE INFORMATION REFER THIS 
[https://leetcode.com/discuss/general-discussion/1296769/min-no-of-swaps-to-sort-the-array-wrong-approach-vs-correct-approach]**

['Tree', 'C++']