-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
27 lines (24 loc) · 848 Bytes
/
index.js
File metadata and controls
27 lines (24 loc) · 848 Bytes
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
const { make_tree } = require('../utils');
const test1 = (make_tree([1,-2,-3,1,3,-2,null,-1], [0]))[0];
/**
* 思路:
*
* 用递归的思想,从叶子节点往上走,如果要往上走,我们只能选择走左边或者右边或者不走,
* 这样我们就遍历了所有的情况,选取里面最大的情况则为最优的结果
*
* @param {TreeNode} root
* @return {number}
*/
var maxPathSum = function(root) {
let result = root.val;
dfs(root);
function dfs(root) {
if (!root) return 0;
const left = dfs(root.left);
const right = dfs(root.right);
result = Math.max(result, root.val, left + root.val, right + root.val, root.val + left + right );
return Math.max(root.val, root.val + left, root.val + right);
}
return result;
};
console.log(maxPathSum(test1));