#K95402. Maximum Depth of Binary Tree

    ID: 38855 Type: Default 1000ms 256MiB

Maximum Depth of Binary Tree

Maximum Depth of Binary Tree

Given a binary tree, your task is to determine its maximum depth. The maximum depth is defined as the number of nodes along the longest path from the root node down to the farthest leaf node. In other words, if the binary tree is represented by a root node root, you need to compute its depth recursively. Formally, if we denote the maximum depth by \(D(root)\), then

\(D(root)=\begin{cases}0,&\text{if } root \text{ is null},\\ 1 + \max(D(root.left),\,D(root.right)),&\text{otherwise}.\end{cases}\)

The binary tree is given as a level-order traversal in the input (see Input section). Note that the value null indicates that there is no node present at that position.

inputFormat

The input consists of a single line containing a comma-separated list of values representing the binary tree in level-order traversal. Each value is either an integer or the string null. For example:

  • Input: null represents an empty tree.
  • Input: 1 represents a tree with only the root node.
  • Input: 1,2,3,4,5,null,null represents the following tree:
            1
           / \
          2   3
         / \
        4   5
        

outputFormat

Output a single integer, the maximum depth of the binary tree.

## sample
null
0