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...