✅✅✅ C++ Solution - explained || Very Simple and Easy to Understand
Approach
Here the trick is to find the count of no of swap, as we can easily get the array of num at a particular level using BFS traversal.
So to evaluate the

# Approach
- Here the trick is to find the count of no of swap, as we can easily get the array of num at a particular level using BFS traversal.
- So to evaluate the no. of count in linear complexity we need to use extra space.
- We will add the level array to a ordered map along with its index as the value
- Now map have the list of nums in sorted order, basically we will get to know the correct index of each element.
- Now traverse through the map and as per the sorted form update the map index when you set the value at its correct position
- If not in sorted form then get original index of value which can be get from the map and swap the elements, so that arr array will be in sorted form gradually.

# Code :
```cpp []
int countSwap(vector<int>& arr){
    map<int, int> reverseIndex;
    for(int i = 0; i < arr.size(); ++i) reverseIndex[arr[i]] = i;
    int i = 0, ans = 0;
    for(auto [k,v] : reverseIndex){
        if(v == i) { i++; continue; }
        reverseIndex[arr[i]] = v;
        swap(arr[i], arr[v]);
        ans++; i++;
    }
    return ans;
}

int minimumOperations(TreeNode* root) {
    int ans = 0;
    queue<TreeNode*> q;
    q.push(root);
    while(q.size()){
        vector<int> arr;
        for(int n = q.size(); n > 0; --n){
            TreeNode* node = q.front(); q.pop();
            arr.push_back(node->val);
            if(node->left) q.push(node->left);
            if(node->right) q.push(node->right);
        }
        ans += countSwap(arr);
    }
    return ans;
}
```

---

<b>Here is an article of my last interview experience - A Journey to FAANG Company, I recomand you to go through this to know which all resources I have used & how I cracked interview at Amazon:
https://leetcode.com/discuss/interview-experience/3171859/Journey-to-a-FAANG-Company-Amazon-or-SDE2-(L5)-or-Bangalore-or-Oct-2022-Accepted

---
['C++']