-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2471.cpp
More file actions
25 lines (25 loc) · 832 Bytes
/
Copy path2471.cpp
File metadata and controls
25 lines (25 loc) · 832 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
public:
int minimumOperations(TreeNode *root) {
queue<TreeNode *> q;
q.push(root);
int res = 0;
while (!q.empty()) {
vector<int> lvl, idx(q.size());
for (int k = q.size(); k > 0; k--) {
TreeNode *root = q.front();
q.pop();
lvl.push_back(root->val);
if (root->left) q.push(root->left);
if (root->right) q.push(root->right);
}
iota(begin(idx), end(idx), 0);
sort(begin(idx), end(idx), [&](const int i, const int j) { return lvl[i] < lvl[j]; });
for (int i = 0; i < idx.size(); i++) {
for (; idx[i] != i; res++)
swap(idx[i], idx[idx[i]]);
}
}
return res;
}
};