C++ 28ms easy to understand solution


  • 0
    P
    int longest=0;
        
        void longestC(TreeNode* root, int value, int curlong)
        {
            if(root==NULL)
                return;
            if(root->val - value==1)
            {
                curlong++;
                if(curlong>longest)
                    longest=curlong;
            }
            else curlong=0;
            
            longestC(root->left,root->val,curlong);
            longestC(root->right,root->val,curlong);
        }
        int longestConsecutive(TreeNode* root) {
            if(root==NULL) return 0;
            longestC(root,root->val,0);
            return longest+1;
        }

Log in to reply
 

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