首页 > 其他 > 详细

LeetCode "Binary Tree Longest Consecutive Sequence"

时间:2015-10-28 12:08:31      阅读:182      评论:0      收藏:0      [点我收藏+]

A simple application to binary tree traversal.

class Solution {
    typedef pair<int, unsigned> Rec; // last val - length
    unsigned ret;
    void go(TreeNode *p, Rec r)
    {
        if(!p) return;
        
        Rec nr(p->val, 1);
        if(r.second && p->val == (r.first + 1))
        {
            nr = Rec(p->val, r.second + 1);
        }
        ret = max(ret, nr.second);
        
        go(p->left, nr);
        go(p->right, nr);
    }
public:
    int longestConsecutive(TreeNode* root) {
        ret = 0;
        go(root, Rec(0, 0));
        return ret;
    }
};

LeetCode "Binary Tree Longest Consecutive Sequence"

原文:http://www.cnblogs.com/tonix/p/4916853.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!