need help. 52/54 tests passed. failed a 10,000 node test case. hard to debug it. any ideas?


  • 0
    V

    Here is my solution. The below solution fails for a 10,000 node test case. Any ideas before I go the ugly route of debugging it step by step with a debugger?

    public class Solution {
        int maxConsecutive = 0;
    
            public int LongestConsecutive(TreeNode root) {
                dfsconsecutive(root);
                return 1 + maxConsecutive;
            }
    
            public int dfsconsecutive(TreeNode t) {
                if (t == null) return 0; //if leaf just return 0
    
                int left = dfsconsecutive(t.left);
                if (t.left != null && t.val + 1 == t.left.val) left++;
    
                int right = dfsconsecutive(t.right);
                if (t.right != null && t.val + 1 == t.right.val) right++;
    
                int count = Math.Max(left, right);
                maxConsecutive = Math.Max(maxConsecutive, count);
                return count;
            }
    }
    

Log in to reply
 

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