-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2471-minimum-number-of-operations-to-sort-a-binary-tree-by-level.js
More file actions
66 lines (58 loc) · 1.82 KB
/
2471-minimum-number-of-operations-to-sort-a-binary-tree-by-level.js
File metadata and controls
66 lines (58 loc) · 1.82 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
/**
* 2471. Minimum Number of Operations to Sort a Binary Tree by Level
* https://leetcode.com/problems/minimum-number-of-operations-to-sort-a-binary-tree-by-level/
* Difficulty: Medium
*
* 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.
*/
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var minimumOperations = function(root) {
let result = 0;
const queue = [root];
const levelValues = [];
while (queue.length) {
const levelSize = queue.length;
const values = [];
for (let i = 0; i < levelSize; i++) {
const node = queue.shift();
values.push(node.val);
if (node.left) queue.push(node.left);
if (node.right) queue.push(node.right);
}
levelValues.push(values);
}
for (const values of levelValues) {
const sorted = [...values].sort((a, b) => a - b);
const indexMap = new Map(values.map((val, i) => [val, i]));
let swaps = 0;
for (let i = 0; i < values.length; i++) {
if (values[i] !== sorted[i]) {
const targetIndex = indexMap.get(sorted[i]);
[values[i], values[targetIndex]] = [values[targetIndex], values[i]];
indexMap.set(values[targetIndex], targetIndex);
indexMap.set(values[i], i);
swaps++;
}
}
result += swaps;
}
return result;
};