Simple C++ solution


  • 0
    G
        int longestConsecutive(TreeNode* root) {
            int max_len = 0;
            _longestConsecutive(root, 0, NULL, &max_len);
            return max_len;
        }
        void _longestConsecutive(TreeNode* root, int len, TreeNode* parent, int* max_len) {
            if (!root) return;
            
            if (!parent || root->val != parent->val + 1) len = 1;
            else len++;
    
            *max_len = max(*max_len, len);
            
            _longestConsecutive(root->left, len, root, max_len);
            _longestConsecutive(root->right, len, root, max_len);
        }
    

Log in to reply
 

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