BFS || Level order Traversal || Easy to understand || Queue
IntuitionApproachComplexity
Time complexity: O(nlogn)

Space complexity: O(n)
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

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

# Complexity
- Time complexity: O(nlogn)
<!-- 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 minSwap(vector<int>& v){
        int cnt = 0;
        vector<int> target = v;
        sort(target.begin(), target.end());

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

        // swap until the correct pos
        for(int i = 0; i<v.size(); i++){
            if(v[i] != target[i]){
                cnt++;
            }

            //update the swap position
            int cur = m[target[i]];
            m[v[i]] = cur;
            swap(v[cur], v[i]);
        }
        return cnt;
    }
    int minimumOperations(TreeNode* root) {
        // BFS
        queue<TreeNode*> q;
        q.push(root);
        int totalCnt = 0;

        // tree level order traversal 
        while(!q.empty()){
            int levelsz = q.size();
            vector<int> level(levelsz);

            for(int i = 0; i<levelsz; i++){
                TreeNode* node = q.front();
                q.pop();

                level[i] = node->val;

                if(node->left != NULL)
                    q.push(node->left);
                if(node->right != NULL){
                    q.push(node->right);
                }
            }
            totalCnt += minSwap(level);
        }
        return totalCnt;
    }
};
```
['Tree', 'Breadth-First Search', 'Binary Tree', 'C++', 'Java', 'Python3']