Skip to content

Commit 48e578b

Browse files
committed
binary-tree-maximum-path-sum solution
1 parent 82cef7c commit 48e578b

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var maxPathSum = function (root) {
2+
let maxSum = -Infinity;
3+
4+
function dfs(node) {
5+
if (node === null) return 0; // 6) Base Case
6+
const left = Math.max(dfs(node.left), 0); // 8) Pruning
7+
const right = Math.max(dfs(node.right), 0); // 8) Pruning
8+
9+
const currentSum = left + node.val + right; // 9) Pivot Sum
10+
maxSum = Math.max(maxSum, currentSum); // 7) Global Max
11+
12+
return node.val + Math.max(left, right); // 10) Return Value
13+
}
14+
15+
dfs(root); // 4) DFS 시작
16+
console.log(maxSum); // 최종 답 출력
17+
};

0 commit comments

Comments
 (0)