diff --git a/2471. Minimum Number of Operations to Sort a Binary Tree by Level b/2471. Minimum Number of Operations to Sort a Binary Tree by Level new file mode 100644 index 0000000..3e2e41d --- /dev/null +++ b/2471. Minimum Number of Operations to Sort a Binary Tree by Level @@ -0,0 +1,37 @@ +/** + * 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) { + int ans = 0; + queue q{{root}}; + while (!q.empty()){ + vector vals; + vector child(q.size()); + for (int size = q.size(); size > 0; size--){ + TreeNode* node = q.front(); + q.pop(); + vals.push_back(node->val); + if (node->left != nullptr) + q.push(node->left); + if (node->right != nullptr) + q.push(node->right); + } + iota(child.begin(), child.end(), 0); + ranges::sort(child, [&vals](int i, int j){ return vals[i] < vals[j]; }); + for (int i = 0; i < child.size(); i++) + for ( ;child[i] != i; ans++) + swap(child[i], child[child[i]]); + } + return ans; + } +};