The "Run Code" & "Submit Solution" show different result for same test case.


  • 0
    L

    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);
            }
        }
        
    }
    

    }


  • 1
    J

    Check this link out: https://leetcode.com/faq/#different-output

    Basically, the way that the test runner works is that it instantiates one instance of the Solution class and runs the longestConsecutive method multiple times. You need to reset your max back to zero every time you run it.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.