美文网首页
Leetcode 298 Binary Tree Longest

Leetcode 298 Binary Tree Longest

作者: stepsma | 来源:发表于2018-09-05 00:07 被阅读0次
class Solution {
public:
    
    int search_util(TreeNode *root){
        if(!root){
            return 0;
        }
        
        int left = search_util(root->left);
        int right = search_util(root->right);
        
        int cur_left = 1, cur_right = 1;
        if(root->left && root->val == root->left->val - 1){
            cur_left = left + 1;
        }
        
        if(root->right && root->val == root->right->val - 1){
            cur_right = right + 1;
        }
        
        int cur_max = max(cur_left, cur_right);
        max_count = max(max_count, cur_max);
        return cur_max;
    }
    
    int longestConsecutive(TreeNode* root) {
        if(!root){
            return 0;
        }
        
        search_util(root);
        return max_count;
    }
private:
    int max_count;
};

相关文章

网友评论

      本文标题:Leetcode 298 Binary Tree Longest

      本文链接:https://www.haomeiwen.com/subject/vlrowftx.html