Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0f27dc3

Browse files
committed
Add solution #2471
1 parentf965dc0 commit0f27dc3

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#1,968 LeetCode solutions in JavaScript
1+
#1,969 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1864,6 +1864,7 @@
18641864
2467|[Most Profitable Path in a Tree](./solutions/2467-most-profitable-path-in-a-tree.js)|Medium|
18651865
2468|[Split Message Based on Limit](./solutions/2468-split-message-based-on-limit.js)|Hard|
18661866
2469|[Convert the Temperature](./solutions/2469-convert-the-temperature.js)|Easy|
1867+
2471|[Minimum Number of Operations to Sort a Binary Tree by Level](./solutions/2471-minimum-number-of-operations-to-sort-a-binary-tree-by-level.js)|Medium|
18671868
2482|[Difference Between Ones and Zeros in Row and Column](./solutions/2482-difference-between-ones-and-zeros-in-row-and-column.js)|Medium|
18681869
2490|[Circular Sentence](./solutions/2490-circular-sentence.js)|Easy|
18691870
2493|[Divide Nodes Into the Maximum Number of Groups](./solutions/2493-divide-nodes-into-the-maximum-number-of-groups.js)|Hard|
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* 2471. Minimum Number of Operations to Sort a Binary Tree by Level
3+
* https://leetcode.com/problems/minimum-number-of-operations-to-sort-a-binary-tree-by-level/
4+
* Difficulty: Medium
5+
*
6+
* You are given the root of a binary tree with unique values.
7+
*
8+
* In one operation, you can choose any two nodes at the same level and swap their values.
9+
*
10+
* Return the minimum number of operations needed to make the values at each level sorted in a
11+
* strictly increasing order.
12+
*
13+
* The level of a node is the number of edges along the path between it and the root node.
14+
*/
15+
16+
/**
17+
* Definition for a binary tree node.
18+
* function TreeNode(val, left, right) {
19+
* this.val = (val===undefined ? 0 : val)
20+
* this.left = (left===undefined ? null : left)
21+
* this.right = (right===undefined ? null : right)
22+
* }
23+
*/
24+
/**
25+
*@param {TreeNode} root
26+
*@return {number}
27+
*/
28+
varminimumOperations=function(root){
29+
letresult=0;
30+
constqueue=[root];
31+
constlevelValues=[];
32+
33+
while(queue.length){
34+
constlevelSize=queue.length;
35+
constvalues=[];
36+
37+
for(leti=0;i<levelSize;i++){
38+
constnode=queue.shift();
39+
values.push(node.val);
40+
if(node.left)queue.push(node.left);
41+
if(node.right)queue.push(node.right);
42+
}
43+
44+
levelValues.push(values);
45+
}
46+
47+
for(constvaluesoflevelValues){
48+
constsorted=[...values].sort((a,b)=>a-b);
49+
constindexMap=newMap(values.map((val,i)=>[val,i]));
50+
letswaps=0;
51+
52+
for(leti=0;i<values.length;i++){
53+
if(values[i]!==sorted[i]){
54+
consttargetIndex=indexMap.get(sorted[i]);
55+
[values[i],values[targetIndex]]=[values[targetIndex],values[i]];
56+
indexMap.set(values[targetIndex],targetIndex);
57+
indexMap.set(values[i],i);
58+
swaps++;
59+
}
60+
}
61+
62+
result+=swaps;
63+
}
64+
65+
returnresult;
66+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp