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 Consecutive Sequence
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 原题 给一个二叉树,求其中最长连续序列的长度 样例比如,下面的树,最长序列为3->4->5,返回3 解题思路 递归...
- 不得不说,Tree的题还算挺有意思的。首先给出two pass的做法: pass one找往下的sequence,...
- https://leetcode.com/problems/binary-tree-longest-consecu...
- Given a binary tree, find the length of the longest conse...