We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 82cef7c commit 48e578bCopy full SHA for 48e578b
binary-tree-maximum-path-sum/moonjonghoo.js
@@ -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