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

**Test case 2:**

10

/ \

20 30

/ \ /

40 60 90

For the above test case no sequence is possible: -1

**Note:**The **Input/Ouput** format and **Example** given are used for system's internal purpose, and should be used by a user for **Expected Output** only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.

Author: harshitsidhwa

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

the_coder95 | 465 |

thanuvinu94 | 388 |

adityasuman2025 | 348 |

Senbonzakura_Kageyoshi | 321 |

PrakharBansal1 | 294 |

the_coder95 | 1691 |

RishabhTanwar1 | 1110 |

thanuvinu94 | 1064 |

samay_kam_hai | 726 |

adityasuman2025 | 646 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4897 |

Quandray | 4547 |

Login to report an issue on this page.