File tree Expand file tree Collapse file tree 1 file changed +21
-20
lines changed
src/main/java/com/fishercoder/solutions Expand file tree Collapse file tree 1 file changed +21
-20
lines changed Original file line number Diff line number Diff line change @@ -29,28 +29,29 @@ The longest consecutive path need to be from parent to child (cannot be the reve
29
29
*/
30
30
public class _298 {
31
31
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 ;
37
41
}
38
- dfs (root , 0 , root .val );
39
- return max ;
40
- }
41
42
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 );
45
55
}
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 );
54
56
}
55
-
56
57
}
You can’t perform that action at this time.
0 commit comments