forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary-tree-longest-consecutive-sequence.cpp
More file actions
39 lines (35 loc) · 1.03 KB
/
binary-tree-longest-consecutive-sequence.cpp
File metadata and controls
39 lines (35 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Time: O(n)
// Space: O(h)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int longestConsecutive(TreeNode* root) {
int max_len = 0;
longestConsecutiveHelper(root, &max_len);
return max_len;
}
int longestConsecutiveHelper(TreeNode *root, int *max_len) {
if (!root) {
return 0;
}
const int left_len = longestConsecutiveHelper(root->left, max_len);
const int right_len = longestConsecutiveHelper(root->right, max_len);
int cur_len = 1;
if (root->left && root->left->val == root->val + 1) {
cur_len = max(cur_len, left_len + 1);
}
if (root->right && root->right->val == root->val + 1) {
cur_len = max(cur_len, right_len + 1);
}
*max_len = max(*max_len, max(cur_len, max(left_len, right_len)));
return cur_len;
}
};