Could anyone help me find the bugs in my code. It is so weird that same test case in "Run code" and "submit solution" gave me different results.

```
public class Solution {
private static int max = 0;
public int longestConsecutive(TreeNode root) {
if (root == null) {
return max;
}
dfs(root, 1);
return max;
}
public static void dfs(TreeNode node, int len) {
if (node.left == null && node.right == null) {
max = Math.max(len, max);
return;
}
if (node.left != null) {
if (node.left.val != node.val + 1) {
max = Math.max(max, len);
dfs(node.left, 1);
} else {
dfs(node.left, len + 1);
}
}
if (node.right != null) {
if (node.right.val != node.val + 1) {
max = Math.max(max, len);
dfs(node.right, 1);
} else {
dfs(node.right, len + 1);
}
}
}
```

}