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 Longest Consecutive SequenceLeetCode
- Leetcode-Longest Consecutive SequenceLeetCode
- Longest Consecutive Sequence leetcode javaLeetCodeJava
- LeetCode128:Longest Consecutive SequenceLeetCode
- Leetcode Binary Tree PathsLeetCode
- LeetCode Invert Binary TreeLeetCode
- Leetcode Balanced Binary TreeLeetCode
- LeetCode-Binary Tree PathsLeetCode
- leetcode - Binary Tree Preorder TraversalLeetCode
- Leetcode Binary Tree Inorder TraversalLeetCode
- Leetcode Binary Tree Preorder TraversalLeetCode
- Leetcode Binary Tree Postorder TraversalLeetCode
- Leetcode Maximum Depth of Binary TreeLeetCode
- Leetcode-Balanced Binary TreeLeetCode
- Balanced Binary Tree leetcode javaLeetCodeJava
- ACM Longest Repeated SequenceACM
- LeetCode545.Boundary-of-Binary-TreeLeetCode
- Binary Tree Level Order Traversal [LEETCODE]LeetCode
- [leetcode]maximum-depth-of-binary-treeLeetCode
- [leetcode]binary-tree-inorder-traversalLeetCode
- [LeetCode] 543. Diameter of Binary TreeLeetCode
- LeetCode 543. Diameter of Binary TreeLeetCode
- LeetCode-Serialize and Deserialize Binary TreeLeetCode
- Leetcode Binary Tree Level Order TraversalLeetCode
- Leetcode Validate Binary Search TreeLeetCode
- Leetcode Minimum Depth of Binary TreeLeetCode
- Leetcode Binary Tree Maximum Path SumLeetCode
- Leetcode-Binary Tree Postorder TraversalLeetCode
- Leetcode-Binary Tree Inorder TraversalLeetCode
- Leetcode-Maximum Depth of Binary TreeLeetCode
- leetcode 之 Recover Binary Search TreeLeetCode
- Recover Binary Search Tree leetcode javaLeetCodeJava
- Binary Tree Inorder Traversal leetcode javaLeetCodeJava
- Binary Tree Preorder Traversal leetcode javaLeetCodeJava
- Binary Tree Postorder Traversal leetcode javaLeetCodeJava
- Maximum Depth of Binary Tree leetcode javaLeetCodeJava
- LeetCode 94 [Binary Tree Inorder Traversal]LeetCode
- LeetCode之Univalued Binary Tree(Kotlin)LeetCodeKotlin