#C8137. Tree Height and Leaf Count
Tree Height and Leaf Count
Tree Height and Leaf Count
You are given a binary tree defined by a set of nodes and edges. The tree is rooted at node 1. For each edge, you are given three integers u, v and c where c indicates whether v is the left child (when c = 0) or the right child (when c = 1) of u.
Your task is to construct the binary tree from the given description, and then compute two quantities for the tree:
- The height of the tree, defined as the number of edges in the longest path from the root to a leaf. In mathematical terms, if the height of a node is \(h\) then the height of the tree is \(\max_{leaf}(h)\).
- The number of leaf nodes in the tree. </p>
- An integer
N
representing the number of nodes in the tree. N-1
lines each containing three integersu
,v
, andc
separated by spaces, representing an edge from nodeu
to nodev
. Here,c = 0
indicates thatv
is the left child andc = 1
indicates thatv
is the right child ofu
.
Note: A single node is considered a leaf, and its height is defined to be 0.
inputFormat
The input starts with an integer T
indicating the number of test cases. Each test case is described as follows:
All input is read from standard input (stdin).
outputFormat
For each test case, output two integers on a single line: the height of the tree and the number of leaf nodes, separated by a space. The output for all test cases should be printed on standard output (stdout), each on a new line.
## sample1
1
0 1
</p>