diff --git a/leetcode/2401-2500/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/1.png b/leetcode/2401-2500/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/1.png new file mode 100644 index 000000000..d0ddd7c67 Binary files /dev/null and b/leetcode/2401-2500/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/1.png differ diff --git a/leetcode/2401-2500/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/2.png b/leetcode/2401-2500/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/2.png new file mode 100644 index 000000000..428c908ba Binary files /dev/null and b/leetcode/2401-2500/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/2.png differ diff --git a/leetcode/2401-2500/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/3.png b/leetcode/2401-2500/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/3.png new file mode 100644 index 000000000..5beecc724 Binary files /dev/null and b/leetcode/2401-2500/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/3.png differ diff --git a/leetcode/2401-2500/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/README.md b/leetcode/2401-2500/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/README.md index 98ad794c6..0c4ba39a6 100755 --- a/leetcode/2401-2500/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/README.md +++ b/leetcode/2401-2500/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/README.md @@ -1,28 +1,53 @@ # [2471.Minimum Number of Operations to Sort a Binary Tree by Level][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +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 1:** +![example1](./1.png) ``` -Input: a = "11", b = "1" -Output: "100" +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:** -## 题解 +![example2](./2.png) -### 思路1 -> ... -Minimum Number of Operations to Sort a Binary Tree by Level -```go ``` +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:** + +![example3](./3.png) +``` +Input: root = [1,2,3,4,5,6] +Output: 0 +Explanation: Each level is already sorted in increasing order so return 0. +``` ## 结语 diff --git a/leetcode/2401-2500/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/Solution.go b/leetcode/2401-2500/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/Solution.go index d115ccf5e..705bb7f89 100644 --- a/leetcode/2401-2500/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/Solution.go +++ b/leetcode/2401-2500/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/Solution.go @@ -1,5 +1,65 @@ package Solution -func Solution(x bool) bool { - return x +import ( + "sort" +) + +type TreeNode struct { + Val int + Left, Right *TreeNode +} + +func minimumCostSort(nums []int) int { + b := make([]int, len(nums)) + rightPos := make(map[int]int) + copy(b, nums) + sort.Ints(b) + for i, n := range b { + rightPos[n] = i + } + swapCount := 0 + idx := 0 + for idx < len(nums) { + if nums[idx] == b[idx] { + idx++ + continue + } + correctPos := rightPos[nums[idx]] + nums[idx], nums[correctPos] = nums[correctPos], nums[idx] + swapCount++ + + } + /* + for idx := 0; idx < len(nums); idx++ { + if nums[idx] == b[idx] { + continue + } + correctPos := rightPos[nums[idx]] + nums[idx], nums[correctPos] = nums[correctPos], nums[idx] + swapCount++ + } + */ + return swapCount +} + +func Solution(root *TreeNode) int { + ans := 0 + queue := []*TreeNode{root} + for len(queue) > 0 { + next := make([]*TreeNode, 0) + values := make([]int, 0) + for _, item := range queue { + if item.Left != nil { + next = append(next, item.Left) + values = append(values, item.Left.Val) + } + if item.Right != nil { + next = append(next, item.Right) + values = append(values, item.Right.Val) + } + } + ans += minimumCostSort(values) + queue = next + } + return ans } diff --git a/leetcode/2401-2500/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/Solution_test.go b/leetcode/2401-2500/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/Solution_test.go index 14ff50eb4..fa8f676e0 100644 --- a/leetcode/2401-2500/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/Solution_test.go +++ b/leetcode/2401-2500/2471.Minimum-Number-of-Operations-to-Sort-a-Binary-Tree-by-Level/Solution_test.go @@ -10,12 +10,52 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs *TreeNode + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", &TreeNode{ + Val: 1, + Left: &TreeNode{ + Val: 4, + Left: &TreeNode{Val: 7}, + Right: &TreeNode{Val: 6}, + }, + Right: &TreeNode{ + Val: 3, + Left: &TreeNode{ + Val: 8, + Left: &TreeNode{Val: 9}, + }, + Right: &TreeNode{Val: 5, Left: &TreeNode{Val: 10}}, + }, + }, 3}, + {"TestCase2", &TreeNode{ + Val: 1, + Left: &TreeNode{ + Val: 3, + Left: &TreeNode{Val: 7}, + Right: &TreeNode{Val: 6}, + }, + Right: &TreeNode{ + Val: 2, + Left: &TreeNode{ + Val: 5, + }, + Right: &TreeNode{Val: 4}, + }, + }, 3}, + {"TestCase3", &TreeNode{ + Val: 1, + Left: &TreeNode{ + Val: 2, + Left: &TreeNode{Val: 4}, + Right: &TreeNode{Val: 5}, + }, + Right: &TreeNode{ + Val: 3, + Left: &TreeNode{Val: 6}, + }, + }, 0}, } // 开始测试 @@ -30,10 +70,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }