2471. Minimum Number of Operations to Sort a Binary Tree by Level
Special Thanks To CodeStoryWithMIKIntuitionThe task requires sorting the values of the binary tree at each level such that the minimum number of swaps is requir
Special Thanks To CodeStoryWithMIK
---

# Intuition
The task requires sorting the values of the binary tree at each level such that the minimum number of swaps is required to do so. At each level, we can treat the tree as a list of nodes and apply a sorting algorithm to determine the number of swaps needed. This can be done by comparing the original order of the nodes with the sorted order and counting how many swaps are needed.

# Approach
1. **Level-wise Traversal**:
   - Perform a level-order traversal of the tree using a queue.
   - For each level, collect the values of the nodes in an array.
   
2. **Count Minimum Swaps**:
   - For each level, find the minimum number of swaps required to sort the array of node values. This can be achieved by finding the index of each element in the sorted version of the array and swapping them accordingly.
   
3. **Summing Up**:
   - Accumulate the number of swaps required at each level to get the total number of swaps for the entire tree.

# Complexity
- **Time Complexity**:  
  \(O(n \log n)\) where \(n\) is the number of nodes in the tree. The primary complexity arises from sorting the node values at each level.

- **Space Complexity**:  
  \(O(n)\), for storing the values of the nodes at each level and maintaining the queue.

# Code
```cpp
// Thank you CodeStoryWithMIK

class Solution {
public:
    int getMinSwaps(vector<int>& vec) {
        int swaps = 0;
        vector<int> sortedVec = vec;
        sort(begin(sortedVec), end(sortedVec));

        unordered_map<int, int> mp;
        for (int i = 0; i < vec.size(); i++) {
            mp[vec[i]] = i;
        }

        for (int i = 0; i < vec.size(); i++) {
            if (vec[i] != sortedVec[i]) {
                
                int curIdx = mp[sortedVec[i]];
                mp[vec[i]] = curIdx;
                swap(vec[curIdx], vec[i]);

                swaps++;
            }
        }
        return swaps;
    }

    int minimumOperations(TreeNode* root) {
        queue<TreeNode*> que;
        que.push(root);
        int swaps = 0;

        while (!que.empty()) {
            int n = que.size();
            vector<int> levelNodes;

            while(n--) {
                TreeNode* node = que.front();
                que.pop();
                levelNodes.push_back(node->val);

                if (node->left) 
                    que.push(node->left);
                if (node->right)
                    que.push(node->right);
            }

            swaps += getMinSwaps(levelNodes);
        }
        return swaps;
    }
};
```

# Explanation of Code
1. **getMinSwaps Function**:
   - This function calculates the minimum number of swaps needed to sort an array. It first creates a sorted version of the input array, then uses a hashmap to track the current index of each element. It iterates through the array, checking if each element is in its correct position; if not, it performs a swap and counts it.
   
2. **minimumOperations Function**:
   - This function performs a level-order traversal of the binary tree using a queue. For each level, it collects the node values into a vector and passes them to the `getMinSwaps` function to calculate the number of swaps needed for that level. It accumulates the total swaps for the entire tree and returns the result.

---

##### Hope this helps!!
['C++']