🚀Easy C++ Solution [Level Order Traversal + Selection Sort]🚀
IntuitionMy first thought is to use level order traversal to process each level of the binary tree separately. By sorting each level individually, we can count
# Intuition
My first thought is to use level order traversal to process each level of the binary tree separately. By sorting each level individually, we can count the number of swaps needed to sort the values at each level.

# Approach
1. **Level Order Traversal**: Use a queue to perform level order traversal of the binary tree.

2. **Count Swaps**: For each level, use a selection sort algorithm to count the number of swaps needed to sort the values.

3. **Return Result**: Sum the swaps for all levels to get the total number of swaps needed.

# Complexity
- **Time complexity**: $$O(n^2)$$, where $$n$$ is the number of nodes in the tree. This is because selection sort has a time complexity of $$O(n^2)$$.

- **Space complexity**: $$O(n)$$, where $$n$$ is the number of nodes in the tree. This is due to the space needed for the queue and the level vector.

# 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 {
private:
    void countSwaps(vector<int> &level, int &swaps){
        int n = level.size();
        // selection sort
        for(int i = 0;i<n;i++){
            int minIndex = i;
            for(int j = i; j<n;j++){
                if(level[j]<level[minIndex]){
                    minIndex = j;
                }
            }
            // if some bigger value is before minIndex then we need to swap values
            if(i!=minIndex){
                swaps++;
                swap(level[i], level[minIndex]);
            }
        }
    }
public:
    int minimumOperations(TreeNode* root) {
        queue<TreeNode*> q;
        int swaps = 0;
        
        q.push(root);

        // level order traversal
        while(!q.empty()){
            int n = q.size();
            vector<int> level;
            for(int i = 0; i<n;i++){
                TreeNode* front = q.front();
                level.push_back(front->val);
                q.pop();
            
                if(front->left) q.push(front->left);
                if(front->right) q.push(front->right);
            }
            // counting swaps
            countSwaps(level, swaps);
        }

        return swaps;
    }
};
```
['Tree', 'Breadth-First Search', 'Binary Tree', 'C++']