4 Lines One Function C++ Really Simple and Readable


  • 0
    class Solution {
    public:
        int longestConsecutive(TreeNode* root, int length = 0, TreeNode* pre = NULL, int result = 0) {
            if(!root) return length;
            result = max(result, length);
    	if(pre && pre->val != root->val-1) length = 0;
    	return max(result, max(longestConsecutive(root->left, length+1, root), longestConsecutive(root->right, length+1, root)));
        }
    };
    

Log in to reply
 

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