#C1221. Maximum Depth of a Binary Tree
Maximum Depth of a Binary Tree
Maximum Depth of a Binary Tree
You are given a binary tree represented by its level order traversal, where the token null
represents a missing node. Your task is to determine the maximum depth of the tree. The maximum depth is defined as the number of nodes along the longest path from the root node down to the farthest leaf node. Mathematically, if we denote the depth of a node using the function \(\text{maxDepth}\), then the recursive relation is given by:
$$\text{maxDepth}(node) = \begin{cases} 0, & \text{if } node \text{ is null}\\[6pt] \max(\text{maxDepth}(node.left),\, \text{maxDepth}(node.right)) + 1, & \text{otherwise} \end{cases}$$
Please read the input from standard input (stdin
) and print the output to standard output (stdout
).
inputFormat
Input is provided as a single line of space-separated tokens representing the level order traversal of the binary tree. Each token is either an integer or the string 'null' (without quotes) indicating that a child is missing. For an empty tree, the input will be a single token 'null'.
outputFormat
Output a single integer which is the maximum depth of the binary tree.## sample
null
0