-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreeMaximumPathSumV3.js
More file actions
47 lines (40 loc) · 1.22 KB
/
BinaryTreeMaximumPathSumV3.js
File metadata and controls
47 lines (40 loc) · 1.22 KB
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
38
39
40
41
42
43
44
45
46
47
// https://leetcode-cn.com/problems/binary-tree-maximum-path-sum/
var Test = require('./Common/Test');
var { BinaryTree: Tree } = require('./Common/BinaryTree');
// Tree.prototype.sum = 0;
// Tree.prototype.max = 0;
// const value = node.val + Math.max(0, lValue, rValue);
// const max = Math.max(node.val + Math.max(0, lValue) + Math.max(0, rValue), lMax, rMax);
var maxPathSum = function (root) {
const stack = [root];
const valueStack = [];
// const result = [];
let max = -Infinity;
while (stack.length > 0) {
const node = stack[stack.length - 1];
if (node) {
if (node.left || node.right) {
stack.push(node.right);
stack.push(node.left);
node.left = null;
node.right = null;
// node.
}
else {
// result.push(node.val);
// max = Math.max(max, node.val);
stack.pop();
}
}
else {
// valueStack.push()
stack.pop();
}
}
// return result;
};
function test(root) {
Test.test(maxPathSum, Tree.fromArray(root));
}
test([1, 2, 3]);
// test([-10, 9, 20, null, null, 15, 7]);