Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5725 |

@blackshadows | 5685 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4908 |

@saiujwal13083 | 4156 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3511 |

@sushant_a | 3453 |

@KshamaGupta | 3318 |

@the_coder95 | 3302 |

Complete Leaderboard | |

Handle | Score |

@avaiesshaik782 | 786 |

@arpitsatnalika | 777 |

@tujhekya | 602 |

@prakharxyz | 601 |

@code_it_ | 591 |

@iamronak | 505 |

@imnm | 500 |

@pradeepkumarjad | 488 |

@satyam0460 | 481 |

@nitesha125 | 476 |

@Rahul Jadhav 1 | 466 |

Complete Leaderboard |

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 :1 / \ 2 3Output:2Explanation :Longest sequence is 1, 2.

**Example 2:**

Input :10 / \ 20 30 / \ / 40 60 90Output :-1Explanation: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)

**Constraints:**

1 ≤ N ≤ 10^3

Login 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?

Yes
Longest consecutive sequence in Binary tree

...

Please choose 'ReadOnlyMode' if you needn't to 'Edit' the problem e.g. viewing OJ's solution, TestCase Files (TCFs), TimeLimit etc.