-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathSolution.java
More file actions
49 lines (42 loc) · 1.04 KB
/
Solution.java
File metadata and controls
49 lines (42 loc) · 1.04 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
48
49
package BinaryTreeMaximumPathSum;
import commons.datastructures.TreeNode;
/**
* User: Danyang
* Date: 1/27/2015
* Time: 21:18
*
* Given a binary tree, find the maximum path sum.
The path may start and end at any node in the tree.
For example:
Given the below binary tree,
1
/ \
2 3
Return 6.
*/
public class Solution {
int gmax = Integer.MIN_VALUE;
/**
* Notice:
* 1. max path end with a node
* 2. global max may cross a node
* @param root
* @return
*/
public int maxPathSum(TreeNode root) {
if(root==null)
return 0;
getMaxPathEndWithCur(root);
return gmax;
}
int getMaxPathEndWithCur(TreeNode cur) {
if(cur==null)
return 0;
int l = getMaxPathEndWithCur(cur.left);
int r = getMaxPathEndWithCur(cur.right);
gmax = Math.max(gmax,
Math.max(l+cur.val+r, Math.max(cur.val, Math.max(l+cur.val, r+cur.val)))
);
return Math.max(cur.val, Math.max(l+cur.val, r+cur.val));
}
}