Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit cd250cd

Browse files
refactor 124
1 parent 514a64d commit cd250cd

File tree

1 file changed

+57
-57
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+57
-57
lines changed

src/main/java/com/fishercoder/solutions/_124.java

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,72 +7,72 @@
77

88
/**
99
* 124. Binary Tree Maximum Path Sum
10-
11-
Given a binary tree, find the maximum path sum.
12-
For this problem, a path is defined as any sequence of nodes from some starting node to any node
13-
in the tree along the parent-child connections.
14-
15-
The path must contain at least one node and does not need to go through the root.
16-
17-
For example:
18-
Given the below binary tree,
19-
20-
1
21-
/ \
22-
2 3
23-
24-
Return 6.
10+
*
11+
* Given a binary tree, find the maximum path sum.
12+
* For this problem, a path is defined as any sequence of nodes from some starting node to any node
13+
* in the tree along the parent-child connections.
14+
*
15+
* The path must contain at least one node and does not need to go through the root.
16+
*
17+
* For example:
18+
* Given the below binary tree,
19+
*
20+
* 1
21+
* / \
22+
* 2 3
23+
*
24+
* Return 6.
2525
*/
2626
public class _124 {
2727

28-
public static class Solution1 {
29-
int max = Integer.MIN_VALUE;
28+
public static class Solution1 {
29+
int max = Integer.MIN_VALUE;
3030

31-
public int maxPathSum(TreeNode root) {
32-
dfs(root);
33-
return max;
34-
}
35-
36-
private int dfs(TreeNode root) {
37-
if (root == null) {
38-
return 0;
39-
}
31+
public int maxPathSum(TreeNode root) {
32+
dfs(root);
33+
return max;
34+
}
4035

41-
int left = Math.max(dfs(root.left), 0);
42-
int right = Math.max(dfs(root.right), 0);
36+
private int dfs(TreeNode root) {
37+
if (root == null) {
38+
return 0;
39+
}
4340

44-
max = Math.max(max, root.val + left + right);
45-
46-
return root.val + Math.max(left, right);
47-
}
48-
}
41+
int left = Math.max(dfs(root.left), 0);
42+
int right = Math.max(dfs(root.right), 0);
4943

50-
public static class Solution2 {
51-
/**
52-
* This one uses a map to cache, but surprisingly, it's 10% slower than all submissions compared
53-
* with solution1
54-
*/
55-
int max = Integer.MIN_VALUE;
44+
max = Math.max(max, root.val + left + right);
5645

57-
public int maxPathSum(TreeNode root) {
58-
Map<TreeNode, Integer> map = new HashMap<>();
59-
dfs(root, map);
60-
return max;
46+
return root.val + Math.max(left, right);
47+
}
6148
}
6249

63-
private int dfs(TreeNode root, Map<TreeNode, Integer> map) {
64-
if (root == null) {
65-
return 0;
66-
}
67-
if (map.containsKey(root)) {
68-
return map.get(root);
69-
}
70-
int left = Math.max(0, dfs(root.left, map));
71-
int right = Math.max(0, dfs(root.right, map));
72-
max = Math.max(max, root.val + left + right);
73-
int pathSum = root.val + Math.max(left, right);
74-
map.put(root, pathSum);
75-
return pathSum;
50+
public static class Solution2 {
51+
/**
52+
* This one uses a map to cache, but surprisingly, it's 10% slower than all submissions compared
53+
* with solution1
54+
*/
55+
int max = Integer.MIN_VALUE;
56+
57+
public int maxPathSum(TreeNode root) {
58+
Map<TreeNode, Integer> map = new HashMap<>();
59+
dfs(root, map);
60+
return max;
61+
}
62+
63+
private int dfs(TreeNode root, Map<TreeNode, Integer> map) {
64+
if (root == null) {
65+
return 0;
66+
}
67+
if (map.containsKey(root)) {
68+
return map.get(root);
69+
}
70+
int left = Math.max(0, dfs(root.left, map));
71+
int right = Math.max(0, dfs(root.right, map));
72+
max = Math.max(max, root.val + left + right);
73+
int pathSum = root.val + Math.max(left, right);
74+
map.put(root, pathSum);
75+
return pathSum;
76+
}
7677
}
77-
}
7878
}

0 commit comments

Comments
 (0)