Home >Backend Development >PHP Tutorial >Minimum Number of Operations to Sort a Binary Tree by Level
2471. Minimum Number of Operations to Sort a Binary Tree by Level
Difficulty: Medium
Topics: Tree, Breadth-First Search, Binary Tree
You are given the root of a binary tree with unique values.
In one operation, you can choose any two nodes at the same level and swap their values.
Return the minimum number of operations needed to make the values at each level sorted in a strictly increasing order.
The level of a node is the number of edges along the path between it and the root node.
Example 1:
Example 2:
Example 3:
Constraints:
Hint:
Solution:
The problem is about sorting the values of a binary tree level by level in strictly increasing order with the minimum number of operations. In each operation, we can swap the values of two nodes that are at the same level. The goal is to determine the minimum number of such operations required to achieve the sorted order.
Let's implement this solution in PHP: 2471. Minimum Number of Operations to Sort a Binary Tree by Level
<?php /** * @param TreeNode $root * @return Integer */ function minimumOperations($root) { ... ... ... /** * go to ./solution.php */ } /** * Function to calculate minimum swaps to sort an array * * @param $arr * @return int */ function minSwapsToSort($arr) { ... ... ... /** * go to ./solution.php */ } ?> <h3> Explanation: </h3> <ol> <li> <p><strong>Step 1: Perform BFS to group nodes by level</strong>:</p> <ul> <li>Start from the root and traverse the tree level by level.</li> <li>For each node, append its value to the corresponding level in the levels array.</li> </ul> </li> <li> <p><strong>Step 2: For each level, calculate the minimum swaps to sort the values</strong>:</p> <ul> <li>Sort the values at each level.</li> <li>Use a cycle-based approach to calculate the minimum swaps required to transform the current level into a sorted level.</li> </ul> </li> <li> <p><strong>Cycle Decomposition</strong>:</p> <ul> <li>For each unsorted element, trace its cycle (i.e., where it should go) and mark elements as visited.</li> <li>For each cycle, the number of swaps required is the length of the cycle minus one.</li> </ul> </li> <li> <p><strong>Return the total number of swaps</strong>:</p> <ul> <li>Sum the swaps needed for each level and return the total.</li> </ul> </li> </ol> <h3> Example Walkthrough: </h3> <h4> Example 1: </h4> <p>Input tree:<br> </p> <pre class="brush:php;toolbar:false"><?php /** * @param TreeNode $root * @return Integer */ function minimumOperations($root) { ... ... ... /** * go to ./solution.php */ } /** * Function to calculate minimum swaps to sort an array * * @param $arr * @return int */ function minSwapsToSort($arr) { ... ... ... /** * go to ./solution.php */ } ?>
Levels:
Level 1: [4, 3]
Level 2: [7, 6, 8, 5]
Level 3: [9, 10]
Total swaps = 1 (Level 1) 2 (Level 2) = 3 swaps.
Output: 3
Input tree:
1 / \ 4 3 / \ / \ 7 6 8 5 \ 9 \ 10
Levels:
Level 1: [3, 2]
Level 2: [7, 6, 5, 4]
Total swaps = 1 (Level 1) 2 (Level 2) = 3 swaps.
Output: 3
Thus, the overall time complexity is O(N log N), which is efficient enough given the constraints.
For the input tree:
<?php /** * @param TreeNode $root * @return Integer */ function minimumOperations($root) { ... ... ... /** * go to ./solution.php */ } /** * Function to calculate minimum swaps to sort an array * * @param $arr * @return int */ function minSwapsToSort($arr) { ... ... ... /** * go to ./solution.php */ } ?>
The output is 3 swaps, as detailed in the example.
This solution efficiently calculates the minimum number of swaps needed to sort each level of the binary tree by using BFS to group nodes by level and cycle decomposition to minimize the number of swaps. The time complexity of O(N log N) is optimal for handling trees with up to 10^5 nodes.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks ?. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
The above is the detailed content of Minimum Number of Operations to Sort a Binary Tree by Level. For more information, please follow other related articles on the PHP Chinese website!