Skip to content

Commit 0596d28

Browse files
committed
❌ [124] tried...
1 parent cdc1703 commit 0596d28

File tree

3 files changed

+122
-1
lines changed

3 files changed

+122
-1
lines changed

124/my_solution.js

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
/**
11+
* @param {TreeNode} root
12+
* @return {number}
13+
*/
14+
function maxPathSum(root) {
15+
let queue = [root], max = root.val;
16+
17+
const exploreToFindMax = (node) => {
18+
if (node === null) return;
19+
console.log(`-> v:${node.val}`)
20+
21+
// let l = node.left, r = node.right;
22+
// if (l !== null) {}
23+
const defaultForNull = -Infinity;
24+
25+
let treeLeftVal = 0, exploreLeftVal = defaultForNull;
26+
if (node.left !== null) {
27+
treeLeftVal = node.left.val;
28+
// console.log("exploreToFindMax -> l")
29+
exploreLeftVal = node.val + exploreToFindMax(node.left);
30+
}
31+
// console.log("treeLeftVal")
32+
// console.log(treeLeftVal)
33+
// console.log("exploreLeftVal")
34+
// console.log(exploreLeftVal)
35+
36+
let treeRightVal = 0, exploreRightVal = defaultForNull;
37+
if (node.right !== null) {
38+
treeRightVal = node.right.val;
39+
console.log("exploreToFindMax -> r")
40+
exploreRightVal = node.val + exploreToFindMax(node.right);
41+
console.log("exploreRightVal")
42+
console.log(exploreRightVal)
43+
}
44+
45+
let treeMax = node.val + treeLeftVal + treeRightVal; // 0 represents not picking any of them
46+
47+
max = Math.max(max, treeMax, exploreLeftVal, exploreRightVal);
48+
console.log("node.val")
49+
console.log(node.val)
50+
console.log("node.val + Math.max(exploreLeftVal, exploreRightVal, 0)")
51+
console.log(node.val + Math.max(exploreLeftVal, exploreRightVal, 0))
52+
return node.val + Math.max(exploreLeftVal, exploreRightVal, 0);
53+
};
54+
55+
while (queue.length > 0) {
56+
let curr = queue.shift(), left = curr.left, right = curr.right;
57+
if (left !== null) queue.push(left);
58+
if (right !== null) queue.push(right);
59+
exploreToFindMax(curr);
60+
}
61+
62+
console.log("max")
63+
console.log(max)
64+
return max;
65+
}
66+
67+
maxPathSum(
68+
new TreeNode(1,
69+
new TreeNode(-2),
70+
new TreeNode(3)
71+
)
72+
)

124/solution.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
/**
11+
* @param {TreeNode} root
12+
* @return {number}
13+
*/
14+
function maxPathSum(root) {
15+
let queue = [root], max = root.val;
16+
17+
const exploreToFindMax = (node) => {
18+
if (node === null) return;
19+
20+
const defaultForNull = -Infinity;
21+
22+
let treeLeftVal = 0, exploreLeftVal = defaultForNull;
23+
if (node.left !== null) {
24+
treeLeftVal = node.left.val;
25+
exploreLeftVal = node.val + exploreToFindMax(node.left);
26+
}
27+
28+
let treeRightVal = 0, exploreRightVal = defaultForNull;
29+
if (node.right !== null) {
30+
treeRightVal = node.right.val;
31+
exploreRightVal = node.val + exploreToFindMax(node.right);
32+
}
33+
34+
let treeMax = node.val + treeLeftVal + treeRightVal; // 0 represents not picking any of them
35+
36+
max = Math.max(max, treeMax, exploreLeftVal, exploreRightVal);
37+
return node.val + Math.max(exploreLeftVal, exploreRightVal, 0);
38+
};
39+
40+
while (queue.length > 0) {
41+
let curr = queue.shift(), left = curr.left, right = curr.right;
42+
if (left !== null) queue.push(left);
43+
if (right !== null) queue.push(right);
44+
exploreToFindMax(curr);
45+
}
46+
47+
return max;
48+
}

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
- [128. Longest Consecutive Sequence](./128/)
4646
- [133. Clone Graph](./133/)
4747
- [135. Candy](./135/)
48+
- [124. Binary Tree Maximum Path Sum](./124/)
4849
- [125. Valid Palindrome](./125/)
4950
- [141. Linked List Cycle](./141/)
5051
- [143. Reorder List](./143/)
@@ -148,7 +149,7 @@ Batch create:
148149
NOTE: JS IS HERE
149150
-->
150151
```ssh
151-
chapter=209 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
152+
chapter=124 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
152153
```
153154
> then you can use `x` for quick debug.
154155

0 commit comments

Comments
 (0)