E608 refactor 298 · nabinkumar/Leetcode@4b769d7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4b769d7

Browse files
refactor 298
1 parent 9d1deb9 commit 4b769d7

File tree

1 file changed

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

1 file changed

+21
-20
lines changed

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

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,29 @@ The longest consecutive path need to be from parent to child (cannot be the reve
2929
*/
3030
public class _298 {
3131

32-
private int max = 1;
33-
34-
public int longestConsecutive(TreeNode root) {
35-
if (root == null) {
36-
return 0;
32+
public static class Solution1 {
33+
private int max = 1;
34+
35+
public int longestConsecutive(TreeNode root) {
36+
if (root == null) {
37+
return 0;
38+
}
39+
dfs(root, 0, root.val);
40+
return max;
3741
}
38-
dfs(root, 0, root.val);
39-
return max;
40-
}
4142

42-
private void dfs(TreeNode root, int curr, int target) {
43-
if (root == null) {
44-
return;
43+
private void dfs(TreeNode root, int curr, int target) {
44+
if (root == null) {
45+
return;
46+
}
47+
if (root.val == target) {
48+
curr++;
49+
} else {
50+
curr = 1;
51+
}
52+
max = Math.max(max, curr);
53+
dfs(root.left, curr, root.val + 1);
54+
dfs(root.right, curr, root.val + 1);
4555
}
46-
if (root.val == target) {
47-
curr++;
48-
} else {
49-
curr = 1;
50-
}
51-
max = Math.max(max, curr);
52-
dfs(root.left, curr, root.val + 1);
53-
dfs(root.right, curr, root.val + 1);
5456
}
55-
5657
}

0 commit comments

Comments
 (0)
0