-
Notifications
You must be signed in to change notification settings - Fork 0
/
124.二叉树中的最大路径和.js
38 lines (34 loc) · 1.04 KB
/
124.二叉树中的最大路径和.js
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
28
29
30
31
32
33
34
35
36
37
/*
* @lc app=leetcode.cn id=124 lang=javascript
*
* [124] 二叉树中的最大路径和
*/
// @lc code=start
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var maxPathSum = function(root) {
let maxSum = Number.MIN_SAFE_INTEGER;
const dfs = (root) => {
if (root === null) return 0;
//计算左边分支最大值,左边分支如果为负数还不如不选择
const leftMax = Math.max(0, dfs(root.left));
//计算右边分支最大值,右边分支如果为负数还不如不选择
const rightMax = Math.max(0, dfs(root.right));
// left->root->right 作为路径与已经计算过历史最大值做比较
maxSum = Math.max(maxSum, root.val + leftMax + rightMax);
return root.val + Math.max(leftMax, rightMax);
}
dfs(root);
return maxSum;
};
// @lc code=end