Longest consecutive sequence in Binary tree
Easy Accuracy: 40.08% Submissions: 9123 Points: 2

Given a Binary Tree find the length of the longest path which comprises of nodes with consecutive values in increasing order. 
Note: Sequence of length 1 won't be considered.

Example 1:

Input :      
         /   \                          
        2     3                      

Output: 2

Explanation : Longest sequence is 1, 2.

Example 2:

Input :    
         /     \                          
       20       30                      
      /  \      /
     40      60   90    

Output :  -1

Explanation: For the above test case no 
sequence is possible. So output is -1.

Your Task:  
You dont need to read input or print anything. Complete the function longestConsecutive() which takes root node as input parameter and returns the length of the longest consecutive increasing sequence (of at least
length 2) in the given Binary Tree. If such a sequence does not exist, it returns -1.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of Tree)

1 ≤ N ≤ 10^3

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.