search
HomeBackend DevelopmentPHP TutorialMinimum 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:

Minimum Number of Operations to Sort a Binary Tree by Level

  • Input: root = [1,4,3,7,6,8,5,null,null,null,null,9,null,10]
  • Output: 3
  • Explanation:
    • Swap 4 and 3. The 2nd level becomes [3,4].
    • Swap 7 and 5. The 3rd level becomes [5,6,8,7].
    • Swap 8 and 7. The 3rd level becomes [5,6,7,8].
    • We used 3 operations so return 3.
    • It can be proven that 3 is the minimum number of operations needed.

Example 2:

Minimum Number of Operations to Sort a Binary Tree by Level

  • Input: root = [1,3,2,7,6,5,4]
  • Output: 3
  • Explanation:
    • Swap 3 and 2. The 2nd level becomes [2,3].
    • Swap 7 and 4. The 3rd level becomes [4,6,5,7].
    • Swap 6 and 5. The 3rd level becomes [4,5,6,7].
    • We used 3 operations so return 3.
    • It can be proven that 3 is the minimum number of operations needed.

Example 3:

Minimum Number of Operations to Sort a Binary Tree by Level

  • Input: root = [1,2,3,4,5,6]
  • Output: 0
  • Explanation: Each level is already sorted in increasing order so return 0.

Constraints:

  • The number of nodes in the tree is in the range [1, 105].
  • 1 5
  • All the values of the tree are unique.

Hint:

  1. We can group the values level by level and solve each group independently.
  2. Do BFS to group the value level by level.
  3. Find the minimum number of swaps to sort the array of each level.
  4. While iterating over the array, check the current element, and if not in the correct index, replace that element with the index of the element which should have come.

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.

Key Points:

  1. Binary Tree Levels: Each level of the binary tree should have values sorted in strictly increasing order.
  2. Unique Values: All nodes have unique values, making sorting easier as there are no duplicates.
  3. BFS for Level Grouping: Use Breadth-First Search (BFS) to traverse the tree and group nodes by their levels.
  4. Minimum Swaps: For each level, we need to find the minimum number of swaps required to sort the node values at that level.
  5. Constraints: The tree can have up to 10^5 nodes, so the solution must be efficient.

Approach:

  1. BFS Traversal: Perform a BFS to traverse the tree and collect the values of nodes at each level.
  2. Sorting Each Level: For each level, calculate the minimum number of swaps to sort the values in strictly increasing order.
  3. Cycle Decomposition: The key observation to minimize swaps is that sorting an array can be seen as swapping elements in cycles. The minimum number of swaps for each cycle of misplaced elements is the length of the cycle minus one.
  4. Accumulating Swaps: Sum the swaps for each level to get the total number of swaps required.

Plan:

  1. BFS: Traverse the tree using BFS to collect nodes at each level.
  2. Sorting Each Level: Sort the values at each level and compute the minimum number of swaps.
  3. Calculate Swaps: Use cycle decomposition to find the minimum swaps needed to sort the nodes at each level.

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
     */
}
?>

Explanation:

  1. Step 1: Perform BFS to group nodes by level:

    • Start from the root and traverse the tree level by level.
    • For each node, append its value to the corresponding level in the levels array.
  2. Step 2: For each level, calculate the minimum swaps to sort the values:

    • Sort the values at each level.
    • Use a cycle-based approach to calculate the minimum swaps required to transform the current level into a sorted level.
  3. Cycle Decomposition:

    • For each unsorted element, trace its cycle (i.e., where it should go) and mark elements as visited.
    • For each cycle, the number of swaps required is the length of the cycle minus one.
  4. Return the total number of swaps:

    • Sum the swaps needed for each level and return the total.

Example Walkthrough:

Example 1:

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
     */
}
?>

Levels:

  • Level 0: [1]
  • Level 1: [4, 3]
  • Level 2: [7, 6, 8, 5]
  • Level 3: [9, 10]
  1. Level 1: [4, 3]

    • Sort it to [3, 4] with 1 swap (swap 4 and 3).
  2. Level 2: [7, 6, 8, 5]

    • Sort it to [5, 6, 7, 8] with 2 swaps (swap 7 and 5, swap 8 and 7).
  3. Level 3: [9, 10]

    • Already sorted, no swaps needed.

Total swaps = 1 (Level 1) 2 (Level 2) = 3 swaps.

Output: 3

Example 2:

Input tree:

        1
       / \
      4   3
     / \ / \
    7  6 8  5
             \
              9
               \
                10

Levels:

  • Level 0: [1]
  • Level 1: [3, 2]
  • Level 2: [7, 6, 5, 4]
  1. Level 1: [3, 2]

    • Sort it to [2, 3] with 1 swap (swap 3 and 2).
  2. Level 2: [7, 6, 5, 4]

    • Sort it to [4, 5, 6, 7] with 2 swaps (swap 7 and 4, swap 6 and 5).

Total swaps = 1 (Level 1) 2 (Level 2) = 3 swaps.

Output: 3

Time Complexity:

  1. BFS: O(N) where N is the number of nodes in the tree.
  2. Sorting Each Level: Sorting the values at each level takes O(L log L) where L is the number of nodes at the current level. In the worst case, the total sorting complexity is O(N log N).
  3. Cycle Decomposition: O(L) for each level.

Thus, the overall time complexity is O(N log N), which is efficient enough given the constraints.

Output for Example:

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:

  • LinkedIn
  • GitHub

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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)