deno.land / x / masx200_leetcode_test@10.6.5 / binary-tree-maximum-path-sum / index.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { TreeNode } from "../binary-tree-inorder-traversal/TreeNode.ts";
function maxPathSum(root: TreeNode | null): number { let maxSum = Number.MIN_SAFE_INTEGER; // 最大路径和
dfs(root, (a) => maxSum = Math.max(maxSum, a)); // 递归的入口
return maxSum;}export default maxPathSum;function dfs(root: TreeNode | null, o: (a: number) => void): number { if (root == null) { // 遍历到null节点,收益0 return 0; } const left = dfs(root.left, o); // 左子树提供的最大路径和 const right = dfs(root.right, o); // 右子树提供的最大路径和
const innerMaxSum = left + root.val + right; // 当前子树内部的最大路径和
// 挑战最大纪录 o(innerMaxSum); const outputMaxSum = root.val + Math.max(0, left, right); // 当前子树对外提供的最大和
// 如果对外提供的路径和为负,直接返回0。否则正常返回 return outputMaxSum < 0 ? 0 : outputMaxSum;}
masx200_leetcode_test

Version Info

Tagged at
a year ago