Geeksforgeeks

Error

×

Leaderboard

Showing:

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

@Ibrahim Nash | 6560 |

@blackshadows | 6400 |

@mb1973 | 5777 |

@Quandray | 5271 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 5074 |

@kirtidee18 | 4356 |

@sanjay05 | 3762 |

@mantu_singh | 3638 |

@gfgaccount | 3601 |

@marius_valentin_dragoi | 3525 |

Complete Leaderboard | |

Handle | Score |

@anish5256 | 991 |

@vimleshpratapsingh321 | 980 |

@skj7 | 871 |

@akhileshkumar562002 | 862 |

@pradeeppatidar1999 | 840 |

@kanewilliamson123 | 837 |

@pd420786 | 813 |

@infilooop | 800 |

@bunnybug320 | 796 |

@smitabose7826 | 784 |

@virgat | 768 |

Complete Leaderboard |

Given a Binary Tree find the length of the longest path which comprises of connected nodes with consecutive values in increasing order.

**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, return -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?

YesLoading...

Longest consecutive sequence in Binary tree

...