Skip to content

Commit 9845a38

Browse files
authored
Update 124-binary-tree-maximum-path-sum.js
1 parent ff5fb9d commit 9845a38

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

124-binary-tree-maximum-path-sum.js

+32
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,35 @@ function traverse(node, obj) {
2626
return node.val + Math.max(left, right)
2727
}
2828

29+
// another
30+
31+
/**
32+
* Definition for a binary tree node.
33+
* function TreeNode(val, left, right) {
34+
* this.val = (val===undefined ? 0 : val)
35+
* this.left = (left===undefined ? null : left)
36+
* this.right = (right===undefined ? null : right)
37+
* }
38+
*/
39+
/**
40+
* @param {TreeNode} root
41+
* @return {number}
42+
*/
43+
const maxPathSum = function(root) {
44+
let res = -Infinity
45+
dfs(root)
46+
return res
47+
48+
function dfs(node) {
49+
if(node == null) return 0
50+
let left = dfs(node.left), right = dfs(node.right)
51+
res = Math.max(
52+
res,
53+
node.val,
54+
node.val + left,
55+
node.val + right,
56+
node.val + left + right,
57+
)
58+
return Math.max(node.val, node.val + left, node.val + right)
59+
}
60+
};

0 commit comments

Comments
 (0)