Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
30 lines (25 loc) · 789 Bytes

File metadata and controls

30 lines (25 loc) · 789 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
public class Solution {
private int max;
public int maxPathSum(TreeNode root) {
max = Integer.MIN_VALUE;
if (root == null) {
return 0;
}
search(root);
return max;
}
private int search(TreeNode node) {
if (node == null) {
return 0;
}
int maxLeft = search(node.left);
int maxRight = search(node.right);
int choice1 = maxLeft + maxRight + node.val;
int choice2 = maxLeft + node.val;
int choice3 = maxRight + node.val;
int choice4 = node.val;
int bestChoice = Math.max(choice1, Math.max(choice2, Math.max(choice3, choice4)));
max = Math.max(max, bestChoice);
return Math.max(choice4, Math.max(choice2, choice3));
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.