/**
* 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:
void helper(TreeNode* root, vector<int> vc,int & maxSeriralCount ){
if(root == NULL){
return ;
}
//printf("d root=%p root->val=%d\n",root,root->val);
if(root->left == NULL && root->right==NULL){
if(vc.size()>0){
if(vc[vc.size()-1] == root->val-1){
vc.push_back(root->val);
}else{
;
}
}else{
vc.push_back(root->val);
}
int vcsize=vc.size();
maxSeriralCount = max(maxSeriralCount,vcsize);
return;
}
if(vc.size()){
if(vc[vc.size()-1] == root->val-1){
vc.push_back(root->val);
int vcsize=vc.size();
maxSeriralCount = max(maxSeriralCount,vcsize);
}
else{
vc.clear();
vc.push_back(root->val);
//return;
}
}
else{
vc.push_back(root->val);
int vcsize=vc.size();
maxSeriralCount = max(maxSeriralCount,vcsize);
}
//printf("d333 root=%p\n",root);
helper(root->left, vc,maxSeriralCount);
helper(root->right,vc,maxSeriralCount);
//printf("d2 root=%p\n",root);
}
int longestConsecutive(TreeNode* root) {
if(root==NULL){
return 0;
}
//printf("root=%p\n",root);
vector<int> vc;
int maxSeriralCount=0;
helper(root,vc,maxSeriralCount);
#if 0
int maxSeriralCountl=0;
//printf("1 root=%p\n",root);
maxSeriralCountl=longestConsecutive(root->left);
int maxSeriralCountr=0;
// printf("2 root=%p\n",root);
maxSeriralCountr=longestConsecutive(root->right);
return max(maxSeriralCount,max(maxSeriralCountr,maxSeriralCountl));
#endif
return maxSeriralCount;
}
};