-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2471-MinimumNumberOfOperationsToSortABinaryTreeByLevel.go
More file actions
216 lines (201 loc) · 6.79 KB
/
2471-MinimumNumberOfOperationsToSortABinaryTreeByLevel.go
File metadata and controls
216 lines (201 loc) · 6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
// 2471. Minimum Number of Operations to Sort a Binary Tree by Level
// 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:
// <img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174006-2.png" />
// 1
// / \
// 4 3
// / \ / \
// 7 6 8 5
// / /
// 9 10
// 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:
// <img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174026-3.png" />
// 1
// / \
// 3 2
// / \ / \
// 7 6 5 4
// 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:
// <img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174052-4.png" />
// 1
// / \
// 2 3
// / \ /
// 4 5 6
// 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, 10^5].
// 1 <= Node.val <= 10^5
// All the values of the tree are unique.
import "fmt"
import "sort"
// Definition for a binary tree node.
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func minimumOperations(root *TreeNode) int {
if root == nil { return 0 }
helper := func(nums []int) int {
sorted := make([]int, len(nums))
copy(sorted, nums)
sort.Ints(sorted)
res, index := 0, make(map[int]int)
for i, v := range nums {
index[v] = i
}
for i := range sorted {
if sorted[i] != nums[i] {
res++
newPos := index[sorted[i]]
index[nums[i]] = newPos
index[nums[newPos]] = i
nums[i], nums[newPos] = nums[newPos], nums[i]
}
}
return res
}
res, queue := 0, []*TreeNode{root}
for len(queue) > 0 {
n := len(queue)
arr := make([]int, 0, n)
for i := 0; i < n; i++ {
top := queue[0]
queue = queue[1:] // pop
arr = append(arr, top.Val)
if top.Left != nil { queue = append(queue, top.Left) }
if top.Right != nil { queue = append(queue, top.Right) }
}
res += helper(arr)
}
return res
}
func minimumOperations1(root *TreeNode) int {
res, queue := 0, []*TreeNode{root}
for len(queue) > 0 {
n := len(queue)
arr := make([]int, n)
tmp := queue
queue = nil
for i, node := range tmp {
arr[i] = node.Val
if node.Left != nil { queue = append(queue, node.Left) }
if node.Right != nil { queue = append(queue, node.Right) }
}
index := make([]int, n)
for i := range index {
index[i] = i
}
sort.Slice(index, func(i, j int) bool {
return arr[index[i]] < arr[index[j]]
})
res += n
visited := make([]bool, n)
for _, v := range index {
if !visited[v] {
for ; !visited[v]; v = index[v] {
visited[v] = true
}
res--
}
}
}
return res
}
func main() {
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174006-2.png" />
// 1
// / \
// 4 3
// / \ / \
// 7 6 8 5
// / /
// 9 10
// 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.
tree1 := &TreeNode {
1,
&TreeNode { 4, &TreeNode { 7, nil, nil }, &TreeNode { 6, nil, nil }, },
&TreeNode { 3, &TreeNode { 8, &TreeNode { 9, nil, nil }, nil }, &TreeNode { 5, &TreeNode { 10, nil, nil }, nil }, },
}
fmt.Println(minimumOperations(tree1)) // 3
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174026-3.png" />
// 1
// / \
// 3 2
// / \ / \
// 7 6 5 4
// 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.
tree2 := &TreeNode {
1,
&TreeNode { 3, &TreeNode { 7, nil, nil }, &TreeNode { 6, nil, nil }, },
&TreeNode { 2, &TreeNode { 5, nil, nil }, &TreeNode { 4, nil, nil }, },
}
fmt.Println(minimumOperations(tree2)) // 3
// Example 3:
// <img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174052-4.png" />
// 1
// / \
// 2 3
// / \ /
// 4 5 6
// Input: root = [1,2,3,4,5,6]
// Output: 0
// Explanation: Each level is already sorted in increasing order so return 0.
tree3 := &TreeNode {
1,
&TreeNode { 2, &TreeNode { 4, nil, nil }, &TreeNode { 5, nil, nil }, },
&TreeNode { 3, &TreeNode { 6, nil, nil }, nil, },
}
fmt.Println(minimumOperations(tree3)) // 0
fmt.Println(minimumOperations1(tree1)) // 3
fmt.Println(minimumOperations1(tree2)) // 3
fmt.Println(minimumOperations1(tree3)) // 0
}