Longest consecutive sequence in Binary tree
PROBLEM :
Given a Binary Tree find the length of the longest path which comprises of nodes with consecutive values in increasing order. Every node is considered as a path of length 1.
Input :
The first line of the input contains a single integer T denoting the number of test cases. For each test a root node is given to the longestConsecutive function. The only input to the function is the root of the binary Tree.
Output:
For each test case output in a single line, find the length of the longest path of the binary tree.
If no such sequence is possible return -1.
Constraints:
1<=T<=50
1<=N<=50
Example:
Input:
2
2
1 2 L 1 3 R
5
10 20 L 10 30 R 20 40 L 20 60 R 30 90 L
Output:
2
-1
Explanation:
Test case 1:
1
/ \
2 3
For the above test case the longest sequence is : 1 2 or 1 3
Test case 2:
10
/ \
20 30
/ \ /
40 60 90
For the above test case no sequence is possible: -1
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*
struct Node
{
int data;
Node *left, *right;
};
*/
// your task is tp complete this function
// function should return the length of the longestConsecutive
// sequence
void count(Node *,int ,int *) ;
int longestConsecutive(Node* root)
{
int curr,ans ;
curr=1 ;
ans=0 ;
count(root,curr,&ans) ;
if(ans==1) return -1 ;
return ans ;
}
void count(Node *root,int curr,int *ans)
{
if(curr>(*ans))
(*ans)=curr ;
if(root==NULL) return ;
if(root->left)
if(root->data+1==root->left->data)
count(root->left,curr+1,&(*ans)) ;
else
count(root->left,1,&(*ans)) ;
if(root->right)
if(root->data+1==root->right->data)
count(root->right,curr+1,&(*ans)) ;
else
count(root->right,1,&(*ans)) ;
}
---------------------------------------------------------------------------------
Given a Binary Tree find the length of the longest path which comprises of nodes with consecutive values in increasing order. Every node is considered as a path of length 1.
Input :
The first line of the input contains a single integer T denoting the number of test cases. For each test a root node is given to the longestConsecutive function. The only input to the function is the root of the binary Tree.
Output:
For each test case output in a single line, find the length of the longest path of the binary tree.
If no such sequence is possible return -1.
Constraints:
1<=T<=50
1<=N<=50
Example:
Input:
2
2
1 2 L 1 3 R
5
10 20 L 10 30 R 20 40 L 20 60 R 30 90 L
Output:
2
-1
Explanation:
Test case 1:
1
/ \
2 3
For the above test case the longest sequence is : 1 2 or 1 3
Test case 2:
10
/ \
20 30
/ \ /
40 60 90
For the above test case no sequence is possible: -1
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*
struct Node
{
int data;
Node *left, *right;
};
*/
// your task is tp complete this function
// function should return the length of the longestConsecutive
// sequence
void count(Node *,int ,int *) ;
int longestConsecutive(Node* root)
{
int curr,ans ;
curr=1 ;
ans=0 ;
count(root,curr,&ans) ;
if(ans==1) return -1 ;
return ans ;
}
void count(Node *root,int curr,int *ans)
{
if(curr>(*ans))
(*ans)=curr ;
if(root==NULL) return ;
if(root->left)
if(root->data+1==root->left->data)
count(root->left,curr+1,&(*ans)) ;
else
count(root->left,1,&(*ans)) ;
if(root->right)
if(root->data+1==root->right->data)
count(root->right,curr+1,&(*ans)) ;
else
count(root->right,1,&(*ans)) ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment