Not able to debug, 52/54 test cases passed


  • 0
    V

    I am not able to find what is the problem in my code, it passes 52 out of 52 cases and fails for a big tree with many nodes. Anyone, please help me if time permits. Thanks.

    int longestConsecutive(TreeNode* root) {
            if (!root)
                return 0;
            int left = 1, right = 1;
            if (root->left)
                left = (root->val + 1 == root->left->val) + longestConsecutive(root->left);
            if (root->right)
                right = (root->val + 1 == root->right->val) + longestConsecutive(root->right);
            return max(left, right);
        }
    

  • 1
    S

    you should also consider maintaining the longest consecutive path you have found so far. Your code however breaks this.

    Try considering

    1
      \
       2
         \
          4
            \
              5

Log in to reply
 

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