|
| 1 | +<h2><a href="https://leetcode.com/problems/minimum-number-of-operations-to-sort-a-binary-tree-by-level/">2471. Minimum Number of Operations to Sort a Binary Tree by Level</a></h2><h3>Medium</h3><hr><div><p>You are given the <code>root</code> of a binary tree with <strong>unique values</strong>.</p> |
| 2 | + |
| 3 | +<p>In one operation, you can choose any two nodes <strong>at the same level</strong> and swap their values.</p> |
| 4 | + |
| 5 | +<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> |
| 6 | + |
| 7 | +<p>The <strong>level</strong> of a node is the number of edges along the path between it and the root node<em>.</em></p> |
| 8 | + |
| 9 | +<p> </p> |
| 10 | +<p><strong class="example">Example 1:</strong></p> |
| 11 | +<img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174006-2.png" style="width: 500px; height: 324px;"> |
| 12 | +<pre><strong>Input:</strong> root = [1,4,3,7,6,8,5,null,null,null,null,9,null,10] |
| 13 | +<strong>Output:</strong> 3 |
| 14 | +<strong>Explanation:</strong> |
| 15 | +- Swap 4 and 3. The 2<sup>nd</sup> level becomes [3,4]. |
| 16 | +- Swap 7 and 5. The 3<sup>rd</sup> level becomes [5,6,8,7]. |
| 17 | +- Swap 8 and 7. The 3<sup>rd</sup> level becomes [5,6,7,8]. |
| 18 | +We used 3 operations so return 3. |
| 19 | +It can be proven that 3 is the minimum number of operations needed. |
| 20 | +</pre> |
| 21 | + |
| 22 | +<p><strong class="example">Example 2:</strong></p> |
| 23 | +<img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174026-3.png" style="width: 400px; height: 303px;"> |
| 24 | +<pre><strong>Input:</strong> root = [1,3,2,7,6,5,4] |
| 25 | +<strong>Output:</strong> 3 |
| 26 | +<strong>Explanation:</strong> |
| 27 | +- Swap 3 and 2. The 2<sup>nd</sup> level becomes [2,3]. |
| 28 | +- Swap 7 and 4. The 3<sup>rd</sup> level becomes [4,6,5,7]. |
| 29 | +- Swap 6 and 5. The 3<sup>rd</sup> level becomes [4,5,6,7]. |
| 30 | +We used 3 operations so return 3. |
| 31 | +It can be proven that 3 is the minimum number of operations needed. |
| 32 | +</pre> |
| 33 | + |
| 34 | +<p><strong class="example">Example 3:</strong></p> |
| 35 | +<img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174052-4.png" style="width: 400px; height: 274px;"> |
| 36 | +<pre><strong>Input:</strong> root = [1,2,3,4,5,6] |
| 37 | +<strong>Output:</strong> 0 |
| 38 | +<strong>Explanation:</strong> Each level is already sorted in increasing order so return 0. |
| 39 | +</pre> |
| 40 | + |
| 41 | +<p> </p> |
| 42 | +<p><strong>Constraints:</strong></p> |
| 43 | + |
| 44 | +<ul> |
| 45 | + <li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li> |
| 46 | + <li><code>1 <= Node.val <= 10<sup>5</sup></code></li> |
| 47 | + <li>All the values of the tree are <strong>unique</strong>.</li> |
| 48 | +</ul> |
| 49 | +</div> |
0 commit comments