Given a Binary Tree of **N** nodes. The task is to find the vertical width of the tree.

The width of a binary tree is the number of vertical paths in that tree.

The above tree contains **6** vertical lines which is the required vertical width of the tree.

**Input:**

First line of input contains number of testcases T. For each testcase, there will be two lines, first of which containing the number of edges (between two nodes) in the tree. Next line contains N pairs (considering **a** and **b**) with a '**L**' (means node b on left of a) or '**R**' (means node b on right of a) after a and b.

**Output:**

For each test case, there will be a single line of output containing the vertical widht of the binary tree.

**User Task:**

The task is to complete the function **verticalWidth**() which takes root as the only parameter, and returns the vertical width of the binary tree.

**Constraints:**

1 <= T <= 100

1 <= N <= 10^{3}

**Example:
Input:**

3

8

1 2 L 1 3 R 2 4 L 2 5 R 3 6 L 3 7 R 6 8 R 7 9 R

2

1 2 L 1 3 R

5

10 20 L 10 30 R 20 40 L 20 60 R 30 90 L

**Output:**

6

3

4

**Explanation:**

See the above given figure.

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.

hari32147 | 100 |

AtakanSarıoğlu | 83 |

geetyadav | 82 |

amitpandey19 | 81 |

AdityaRaj9 | 75 |

abducodes | 862 |

harshtyagi36 | 425 |

vamsikrishna55 | 323 |

sandipbhaumik | 321 |

arpit_anshuman | 319 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4567 |

Login to report an issue on this page.