Binary Tree

3.7

6 votes
Binary Tree, Very-Easy
Problem

Given a binary tree which has T nodes, you need to find the diameter of that binary tree. The diameter of a tree is the number of nodes on the longest path between two leaves in the tree.

Input:
First line contains two integers, T and X, number of nodes in the tree and value of the root.
Next 2×(T1) lines contain details of nodes.
Each detail of node contains two lines. First lines contains a string and second line contains an integer, which denotes the path of the node and the value of the node respectively.

String consists of only L or R. L denotes left child and R denotes right child. ( Look at the sample explanation for more details )

Output:
Print the diameter of the binary tree.

Constraints:
1T20
1valueofnodes20

Sample Input
5 1
L
2
R
3
LL
4
LR
5
Sample Output
4
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Root will be 1
Details of second node is L and its value is 2.
So, Left child of root is 2.
Details of third node is R and its value is 3.
So, Right child of root is 3.
Details of fourth node is LL and its value is 4.
So, Left child of Left child of root is 4.
Details of second node is LR and its value is 5.
So, Right child of Left child of root is 5.

Editor Image

?