Class Solution
java.lang.Object
g2401_2500.s2471_minimum_number_of_operations_to_sort_a_binary_tree_by_level.Solution
2471 - Minimum Number of Operations to Sort a Binary Tree by Level.<p>Medium</p>
<p>You are given the <code>root</code> of a binary tree with <strong>unique values</strong>.</p>
<p>In one operation, you can choose any two nodes <strong>at the same level</strong> and swap their values.</p>
<p>Return <em>the minimum number of operations needed to make the values at each level sorted in a <strong>strictly increasing order</strong></em>.</p>
<p>The <strong>level</strong> of a node is the number of edges along the path between it and the root node_._</p>
<p><strong>Example 1:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174006-2.png" alt="" /></p>
<p><strong>Input:</strong> root = [1,4,3,7,6,8,5,null,null,null,null,9,null,10]</p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Swap 4 and 3. The 2<sup>nd</sup> level becomes [3,4].</li>
<li>Swap 7 and 5. The 3<sup>rd</sup> level becomes [5,6,8,7].</li>
<li>Swap 8 and 7. The 3<sup>rd</sup> level becomes [5,6,7,8].</li>
</ul>
<p>We used 3 operations so return 3. It can be proven that 3 is the minimum number of operations needed.</p>
<p><strong>Example 2:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174026-3.png" alt="" /></p>
<p><strong>Input:</strong> root = [1,3,2,7,6,5,4]</p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Swap 3 and 2. The 2<sup>nd</sup> level becomes [2,3].</li>
<li>Swap 7 and 4. The 3<sup>rd</sup> level becomes [4,6,5,7].</li>
<li>Swap 6 and 5. The 3<sup>rd</sup> level becomes [4,5,6,7].</li>
</ul>
<p>We used 3 operations so return 3. It can be proven that 3 is the minimum number of operations needed.</p>
<p><strong>Example 3:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174052-4.png" alt="" /></p>
<p><strong>Input:</strong> root = [1,2,3,4,5,6]</p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong> Each level is already sorted in increasing order so return 0.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>All the values of the tree are <strong>unique</strong>.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
minimumOperations
-