Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitcd250cd

Browse files
refactor 124
1 parent514a64d commitcd250cd

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
publicclass_124 {
2727

28-
publicstaticclassSolution1 {
29-
intmax =Integer.MIN_VALUE;
28+
publicstaticclassSolution1 {
29+
intmax =Integer.MIN_VALUE;
3030

31-
publicintmaxPathSum(TreeNoderoot) {
32-
dfs(root);
33-
returnmax;
34-
}
35-
36-
privateintdfs(TreeNoderoot) {
37-
if (root ==null) {
38-
return0;
39-
}
31+
publicintmaxPathSum(TreeNoderoot) {
32+
dfs(root);
33+
returnmax;
34+
}
4035

41-
intleft =Math.max(dfs(root.left),0);
42-
intright =Math.max(dfs(root.right),0);
36+
privateintdfs(TreeNoderoot) {
37+
if (root ==null) {
38+
return0;
39+
}
4340

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

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

57-
publicintmaxPathSum(TreeNoderoot) {
58-
Map<TreeNode,Integer>map =newHashMap<>();
59-
dfs(root,map);
60-
returnmax;
46+
returnroot.val +Math.max(left,right);
47+
}
6148
}
6249

63-
privateintdfs(TreeNoderoot,Map<TreeNode,Integer>map) {
64-
if (root ==null) {
65-
return0;
66-
}
67-
if (map.containsKey(root)) {
68-
returnmap.get(root);
69-
}
70-
intleft =Math.max(0,dfs(root.left,map));
71-
intright =Math.max(0,dfs(root.right,map));
72-
max =Math.max(max,root.val +left +right);
73-
intpathSum =root.val +Math.max(left,right);
74-
map.put(root,pathSum);
75-
returnpathSum;
50+
publicstaticclassSolution2 {
51+
/**
52+
* This one uses a map to cache, but surprisingly, it's 10% slower than all submissions compared
53+
* with solution1
54+
*/
55+
intmax =Integer.MIN_VALUE;
56+
57+
publicintmaxPathSum(TreeNoderoot) {
58+
Map<TreeNode,Integer>map =newHashMap<>();
59+
dfs(root,map);
60+
returnmax;
61+
}
62+
63+
privateintdfs(TreeNoderoot,Map<TreeNode,Integer>map) {
64+
if (root ==null) {
65+
return0;
66+
}
67+
if (map.containsKey(root)) {
68+
returnmap.get(root);
69+
}
70+
intleft =Math.max(0,dfs(root.left,map));
71+
intright =Math.max(0,dfs(root.right,map));
72+
max =Math.max(max,root.val +left +right);
73+
intpathSum =root.val +Math.max(left,right);
74+
map.put(root,pathSum);
75+
returnpathSum;
76+
}
7677
}
77-
}
7878
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp