-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsoln.cpp
44 lines (43 loc) · 1.32 KB
/
soln.cpp
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
40
41
42
43
44
/**
* 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 ans = 0;
postorder(root, ans);
return ans;
}
vector<int> postorder(TreeNode* node, int & ans) {
if (node != nullptr) {
auto l = postorder(node->left, ans);
auto r = postorder(node->right, ans);
int l_inc = l[0], l_dec = l[1], r_inc = r[0], r_dec = r[1];
int inc = 1, dec = 1;
if (node->left != nullptr) {
if (node->val + 1 == node->left->val) {
inc = max(inc, l_inc + 1);
} else if (node->val - 1 == node->left->val) {
dec = max(dec, l_dec + 1);
}
}
if (node->right != nullptr) {
if (node->val + 1 == node->right->val) {
inc = max(inc, r_inc + 1);
} else if (node->val - 1 == node->right->val) {
dec = max(dec, r_dec + 1);
}
}
ans = max(ans, inc + dec - 1);
return {inc, dec};
} else {
return {0, 0};
}
}
};