Leetcode 298 Binary Tree Longest Consecutive Sequence
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 Binary Tree PathsLeetCode
- [LeetCode] 226. Invert Binary TreeLeetCode
- [LeetCode] 543. Diameter of Binary TreeLeetCode
- LeetCode 543. Diameter of Binary TreeLeetCode
- Binary Tree Level Order Traversal [LEETCODE]LeetCode
- LeetCode545.Boundary-of-Binary-TreeLeetCode
- Leetcode 226. Invert Binary TreeLeetCode
- [leetcode]binary-tree-inorder-traversalLeetCode
- [leetcode]maximum-depth-of-binary-treeLeetCode
- LeetCode之Univalued Binary Tree(Kotlin)LeetCodeKotlin
- LeetCode之Binary Tree Pruning(Kotlin)LeetCodeKotlin
- LeetCode のminimum-depth-of-binary-treeLeetCode
- LeetCode 104. Maximum Depth of Binary TreeLeetCode
- LeetCode 98. Validate Binary Search TreeLeetCode
- LeetCode | 144. Binary Tree Preorder TraversalLeetCode
- LeetCode | 145. Binary Tree Postorder TraversalLeetCode
- Leetcode 94. Binary Tree Inorder TraversalLeetCode
- Leetcode 144. Binary Tree Preorder TraversalLeetCode
- Leetcode 145. Binary Tree Postorder TraversalLeetCode
- LeetCode#110.Balanced Binary Tree(Tree/Height/DFS/Recursion)LeetCode
- leetcode-124-Binary Tree Maximum Path SumLeetCode
- [LeetCode] 501. Find Mode in Binary Search TreeLeetCode
- [LeetCode] 671. Second Minimum Node In a Binary TreeLeetCode
- LeetCode 124. Binary Tree Maximum Path SumLeetCode
- [leetcode]convert-sorted-array-to-binary-search-treeLeetCode
- LeetCode之Construct String from Binary Tree(Kotlin)LeetCodeStructKotlin
- LeetCode 501. Find Mode in Binary Search TreeLeetCode
- [LeetCode] 2196. Create Binary Tree From DescriptionsLeetCode
- LeetCode C++ 968. Binary Tree Cameras【Tree/DFS】困難LeetCodeC++
- [LeetCode] 109. Convert Sorted List to Binary Search TreeLeetCode
- Java for LeetCode 109 Convert Sorted List to Binary Search TreeJavaLeetCode
- LeetCode 272 Closest Binary Tree Traversal II 解題思路LeetCode
- Traversals of binary tree
- Leetcode 967 Numbers With Same Consecutive DifferencesLeetCode
- LeetCode 545. Boundary of Binary Tree 二叉樹邊界LeetCode二叉樹
- LeetCode 156 Binary Tree Upside Down 上下翻轉二叉樹LeetCodeIDE二叉樹
- Leetcode226.翻轉二叉樹 Invert Binary Tree(Java)LeetCode二叉樹Java
- [LintCode] Check Full Binary Tree