C++ Solution with Visualization ✅ 
IntuitionApproachWhen we use selection sort we select the smallest element ans swap it with the current element which can be used inorder to get minimum number
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
When we use selection sort we select the smallest element ans swap it with the current element which can be used inorder to get minimum number of swaps inorder to make the array sorted.
For each level we will save the array and then count using selection sort.
![Screenshot 2024-12-23 at 10.59.56 PM.png](https://assets.leetcode.com/users/images/23d2c69c-9af4-4d73-840a-8d1805dd161d_1734975118.61788.png)
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity: O(n^2)
<!-- 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 selectionSort(vector<int> &arr) 
    {
        int n = arr.size();
        int count = 0;
        for (int i = 0; i < n - 1; ++i)
        {
            int min_idx = i;
            for (int j = i + 1; j < n; ++j)
            {
                if (arr[j] < arr[min_idx])
                {
                    min_idx = j; 
                }
            }
            if(i != min_idx)
            {
                swap(arr[i], arr[min_idx]);
                count++;
            }
        }
        return count;
    }
    int minimumOperations(TreeNode* root) {
        queue<TreeNode*>q;
        q.push(root);
        q.push(NULL);
        vector<int>temp;
        int count = 0;
        while(!q.empty())
        {
            TreeNode* front = q.front();
            q.pop();
            if(front == NULL)
            {
                count += selectionSort(temp);
                temp.clear();
                if(!q.empty())
                {
                    q.push(NULL);
                }
            }
            else
            {
                temp.push_back(front->val);
                if(front->left)
                {
                    q.push(front->left);
                }
                if(front->right)
                {
                    q.push(front->right);
                }
            }
        }
        return count;
    }
};
```
['Tree', 'Breadth-First Search', 'Binary Tree', 'C++']