-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathBinaryTreeMaxPathSum.java
82 lines (68 loc) · 1.83 KB
/
BinaryTreeMaxPathSum.java
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package problems.leetcode;
/**
* https://leetcode.com/problems/binary-tree-maximum-path-sum/
*/
public class BinaryTreeMaxPathSum {
private static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
@Override
public String toString() {
return "TreeNode{" + "val=" + val + '}';
}
}
private static int maxSum = Integer.MIN_VALUE;
public static int maxPathSum(TreeNode root) {
if (root == null) {
return 0;
}
traverse(root);
return maxSum;
}
private static int traverse(TreeNode node) {
if (node == null) {
return 0;
}
int leftSum = traverse(node.left);
int rightSum = traverse(node.right);
// this node only, this node with left sum, this node with right sum
int maxChildSum = Math.max(node.val, node.val + Math.max(leftSum, rightSum));
maxSum = Math.max(maxChildSum, maxSum);
// sum when this node is root
maxSum = Math.max(node.val + leftSum + rightSum, maxSum);
return maxChildSum;
}
/*
-10
/ \
9 -1
/ / \
30 20 50
/ \
60 1
*
*
*/
public static void main(String[] args) {
TreeNode n_10 = new TreeNode(-10);
TreeNode n9 = new TreeNode(9);
TreeNode n_1 = new TreeNode(-1);
TreeNode n30 = new TreeNode(30);
TreeNode n20 = new TreeNode(20);
TreeNode n50 = new TreeNode(50);
TreeNode n60 = new TreeNode(60);
TreeNode n1 = new TreeNode(1);
n_10.left = n9;
n_10.right = n_1;
n9.left = n30;
n_1.left = n20;
n_1.right = n50;
n20.left = n60;
n20.right = n1;
System.out.println(maxPathSum(n_10));
}
}