#K12076. Binary Tree Depth
Binary Tree Depth
Binary Tree Depth
Given a binary tree where each node is represented by a triplet (i, left, right), where i is the node identifier and left and right are the identifiers of the left and right children respectively (-1
if no child exists), your task is to determine the maximum depth of the binary tree.
The depth of a binary tree is defined as the length of the longest path from the root node (which is guaranteed to be node 1) to any leaf node. Formally, if the depth is denoted by \(D\), then:
\[ D = 1 + \max_{child \in children(root)}(depth(child)) \]It is guaranteed that the tree consists of \(n\) nodes provided in the input.
inputFormat
The first line contains an integer \(n\) which denotes the number of nodes in the binary tree. The following \(n\) lines each contain three integers: i left right
, where i
is the node id, and left
and right
represent the left and right child of node \(i\) respectively. If a child does not exist, its value will be -1.
outputFormat
Output a single integer representing the depth of the binary tree.
## sample5
1 2 3
2 4 -1
3 -1 5
4 -1 -1
5 -1 -1
3
</p>