#C4993. Binary Tree Depth and Leaf Count
Binary Tree Depth and Leaf Count
Binary Tree Depth and Leaf Count
In this problem, you are required to implement a binary search tree with two main functionalities:
- Maximum Depth Calculation: Compute the depth of the tree, where the depth (or height) is defined recursively as $$depth(node)=\begin{cases}0 & \text{if } node \text{ is null}\\1+\max(depth(node.left),\,depth(node.right)) & \text{otherwise}.\end{cases}$$
- Leaf Count: Count the number of leaf nodes in the tree (i.e., nodes with no children).
You will receive a sequence of integers to insert into the tree in the given order. After building the tree, output its maximum depth and the number of leaf nodes.
inputFormat
The input is read from standard input (stdin) and is formatted as follows:
- The first line contains a single integer
n
which represents the number of nodes to be inserted into the tree. - If
n > 0
, the second line containsn
space-separated integers, representing the values to be inserted into the binary tree in the given order. Ifn
is zero, no further input is provided.
outputFormat
The output should be written to standard output (stdout) and must consist of two lines:
- The first line should contain the maximum depth (height) of the binary tree.
- The second line should contain the number of leaf nodes in the binary tree.
0
0
0
</p>