Skip to content

Commit 70fe402

Browse files
committed
✅ [1161] Runtime 176 ms Beats 29.41% Memory 77.1 MB Beats 7.84%
1 parent c2e8f22 commit 70fe402

File tree

3 files changed

+74
-11
lines changed

3 files changed

+74
-11
lines changed

1161/my_solution.js

+9-10
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,45 @@ class TreeNode {
1212
*/
1313
const maxLevelSum = (root) => {
1414
let queue = [root], level = 1, max = root.val, tmpMax = 0,
15-
nbOfLeaves = 0, tmpLeaves = 0, nodesToVisit = 2, visitedCount = 0;
15+
nbOfLeaves = 0, tmpLeaves = 0, nodesToVisit = 2, visitedCount = 0, list = [];
1616

1717
while (queue.length > 0) {
1818
let curr = queue.shift(), left = curr.left, right = curr.right;
1919
console.log(`> curr:${curr.val}`)
2020

2121
if (undefined !== left) {
22+
visitedCount++;
2223
if (null !== left) {
2324
tmpMax += left.val;
2425
queue.push(left);
25-
visitedCount++;
2626
} else {
2727
tmpLeaves++;
2828
}
2929
}
3030

3131
if (undefined !== right) {
32+
visitedCount++;
3233
if (null !== right) {
3334
tmpMax += right.val;
3435
queue.push(right);
35-
visitedCount++;
3636
} else {
3737
tmpLeaves++;
3838
}
3939
}
4040

4141
console.log(`old_level:${level}, tmpMax:${tmpMax}, max:${max}, nodesToVisit:${nodesToVisit} visitedCount:${visitedCount}, tmpLeaves:${tmpLeaves}, nbOfLeaves:${nbOfLeaves}`)
42-
// if (nodesToVisit === (visitedCount + tmpLeaves)) {
4342
if (nodesToVisit === (visitedCount + nbOfLeaves)) {
44-
// console.log(`old_level:${level}, tmpMax:${tmpMax}, max:${max}, visitedCount:${visitedCount}, tmpLeaves:${tmpLeaves}`)
45-
console.log(`>>> tmpMax:${tmpMax}, max:${max}`)
46-
if (tmpMax > max) {
47-
level = (nodesToVisit / 2) + 1;
43+
let tmpLevel = Math.log2(nodesToVisit) + 1;
44+
console.log(`>>> tmpLevel:${tmpLevel}, tmpMax:${tmpMax}, max:${max}`)
45+
46+
if ((null !== tmpMax) && (tmpMax > max)) {
47+
level = tmpLevel;
4848
max = tmpMax;
4949
}
5050

5151
nodesToVisit *= 2;
52-
// visitedCount = nodesToVisit - (tmpLeaves * 2) - nbOfLeaves;
5352
visitedCount = 0;
54-
tmpMax = 0;
53+
tmpMax = null;
5554
nbOfLeaves = (nbOfLeaves + tmpLeaves) * 2;
5655
tmpLeaves = 0;
5756
}

1161/solution.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
class TreeNode {
2+
constructor(val, left, right) {
3+
this.val = (val === undefined ? 0 : val);
4+
this.left = (left === undefined ? null : left);
5+
this.right = (right === undefined ? null : right);
6+
}
7+
}
8+
9+
/**
10+
* @param {TreeNode} root
11+
* @return {number}
12+
*/
13+
const maxLevelSum = (root) => {
14+
let queue = [root], level = 1, max = root.val, tmpMax = 0,
15+
nbOfLeaves = 0, tmpLeaves = 0, nodesToVisit = 2, visitedCount = 0;
16+
17+
while (queue.length > 0) {
18+
let curr = queue.shift(), left = curr.left, right = curr.right;
19+
20+
if (undefined !== left) {
21+
visitedCount++;
22+
if (null !== left) {
23+
tmpMax += left.val;
24+
queue.push(left);
25+
} else {
26+
tmpLeaves++;
27+
}
28+
}
29+
30+
if (undefined !== right) {
31+
visitedCount++;
32+
if (null !== right) {
33+
tmpMax += right.val;
34+
queue.push(right);
35+
} else {
36+
tmpLeaves++;
37+
}
38+
}
39+
40+
if (nodesToVisit === (visitedCount + nbOfLeaves)) {
41+
if ((null !== tmpMax) && (tmpMax > max)) {
42+
level = Math.log2(nodesToVisit) + 1;
43+
max = tmpMax;
44+
}
45+
46+
nodesToVisit *= 2;
47+
nbOfLeaves = (nbOfLeaves + tmpLeaves) * 2;
48+
visitedCount = 0;
49+
tmpLeaves = 0;
50+
tmpMax = null;
51+
}
52+
}
53+
54+
return level;
55+
};

test.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,13 @@
7474
// console.log(set.has(x))
7575

7676
// Map
77-
console.log(new Map([["key", "value"], ["rrsrsr", true]]))
77+
// console.log(new Map([["key", "value"], ["rrsrsr", true]]))
78+
79+
// Array
80+
// let l = [];
81+
// console.log(l)
82+
// l[1] = [1, 2, 3];
83+
// console.log(l)
84+
85+
// Null
86+
console.log(null + 1);

0 commit comments

Comments
 (0)