[C++] Swap efficiently.
BFS to iterate nodes level by level. Then normalize the nodes values, and count the number of swaps to make i = ids[i].Code
BFS to iterate nodes level by level. Then normalize the nodes values, and count the number of swaps to make i = ids[i].
# 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 minimumOperations(TreeNode* root) {
        vector<TreeNode*>lvlNodes, newLevel;
        lvlNodes.push_back(root);
        vector<int>v, ids;
        int result = 0;
        while(!lvlNodes.empty()) {
            for(TreeNode* t: lvlNodes) {
                if (t->left != NULL) {
                    newLevel.push_back(t->left);
                    v.push_back(t->left->val);
                    ids.push_back(v.size() - 1);
                }
                if (t->right != NULL) {
                    newLevel.push_back(t->right);
                    v.push_back(t->right->val);
                    ids.push_back(v.size() - 1);
                }
            }
            sort(ids.begin(), ids.end(), [&](int i, int j) { return v[i] < v[j];});
            for (int i = 0; i < v.size(); i++) {
                while (i != ids[i]) {
                    swap(ids[i], ids[ids[i]]);
                    result++;
                }
            }
            swap(lvlNodes, newLevel);
            while(!newLevel.empty()) {
                newLevel.pop_back();
            }
            while(!v.empty()) {
                v.pop_back();
                ids.pop_back();
            }
        }
        return result;
    }
};
```
['C++']