Skip to content

Commit

Permalink
Create: 124-Binary-Tree-Maximum-Path-Sum.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Ykhan799 authored Sep 7, 2022
1 parent ac3cef1 commit c661398
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions typescript/124-Binary-Tree-Maximum-Path-Sum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/

function maxPathSum(root: TreeNode | null): number {
const res = [root.val];

// return max path sum without split
function dfs(root: TreeNode | null): number {
if (!root) {
return 0;
}

let leftMax = dfs(root.left);
let rightMax = dfs(root.right);
leftMax = Math.max(leftMax, 0);
rightMax = Math.max(rightMax, 0);

// compute max path sum WITH split
res[0] = Math.max(res[0], root.val + leftMax + rightMax);
return root.val + Math.max(leftMax, rightMax);
}

dfs(root);
return res[0];
};

0 comments on commit c661398

Please sign in to comment.