C++ recursive solution

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

Log in to reply

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