124. Binary Tree Maximum Path Sum
Tags:
Hard
Skills:
Tree
June 24, 2025
04:32 AM
No headings found
Loading content...
Related Posts
Leetcode
No headings found
Related Posts
Leetcode
Problem
Approach
Solution (post-order)
1function maxPathSum(root: TreeNode | null): number {
2 let max_sum = Number.MIN_SAFE_INTEGER;
3
4 function dfs(node: TreeNode | null): number {
5 if(node === null) {
6 return 0;
7 }
8 const left_max = Math.max(dfs(node.left), 0);
9 const right_max = Math.max(dfs(node.right), 0);
10
11 max_sum = Math.max(max_sum, node.val + left_max + right_max);
12
13 return node.val + Math.max(left_max, right_max);
14 }
15
16 dfs(root)
17 return max_sum;
18};