Easy to unserstand for beginner /Thought Process Explained
IntuitionBasic Intution we get all elements at level n in single array and then find number of swaps to sort an array. There is Question like it. you may google
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Basic Intution we get all elements at level n in single array and then find number of swaps to sort an array. There is Question like it. you may google it. count total swaps and you got answer

# Approach
<!-- Describe your approach to solving the problem. -->

no of swaps to sort array -> create a array contining elements and its index then sort this array
after that traverse entire and if elemnts in array is equal to element in previous array then move ahead else swap the element on the index where it belongs ( stored with values )
do i-- as we are sending element to its correct element but we don't know that element comming is at its correct position or not.
like this for each error count 1 as swap required and return total swaps to sort an array.


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

- Space complexity: O(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 minSwaps(vector<int>& arr) {
        // Code here
        int n = arr.size(),count = 0;
        vector<pair<int,int>> vec;
        for(int i = 0;i < n;i++){
            vec.push_back({arr[i],i});
        }
        sort(vec.begin(),vec.end());
        for(int i = 0;i < n;i++){
            if(vec[i].first != arr[i]){
                swap(vec[vec[i].second],vec[i]);
                i--;
                count++;
            }
        }
        return count;
    }
    int minimumOperations(TreeNode* root) {
        queue<TreeNode*>q;
        q.push(root);
        int ans = 0;
        while(!q.empty()){
            int size = q.size();
            vector<int> vec;
            for(int i = 0;i < size;i++){
                TreeNode * curr = q.front();q.pop();
                vec.push_back(curr->val);
                if(curr->left)q.push(curr->left);
                if(curr->right)q.push(curr->right);
            }
            ans += minSwaps(vec);
        }
        return ans;
    }
};
```
['Tree', 'Breadth-First Search', 'Sorting', 'Binary Tree', 'C++']