#C10298. Maximum Depth of a Binary Tree

    ID: 39487 Type: Default 1000ms 256MiB

Maximum Depth of a Binary Tree

Maximum Depth of a Binary Tree

You are given a binary tree in level order representation where missing nodes are denoted by the keyword null. Your task is to compute the maximum depth of the binary 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.

Note: The tree is provided as a sequence of values in a single line (from standard input), representing the level order traversal of the tree. For example, the input 3 9 20 null null 15 7 represents the binary tree:

[ 3 /
9 20 /
15 7 ]

The maximum depth of this tree is 3.

Here are some additional examples:

  • Input: 1 null 2 null 3 null 4 — Output: 4
  • Input: 1 — Output: 1
  • Input: 1 2 3 4 5 — Output: 3
  • Input: 1 2 5 3 null null null 4 — Output: 4

inputFormat

The input is given via standard input (stdin) as a single line containing space-separated tokens. Each token can be an integer representing a node's value or the string "null" to indicate a missing node. The binary tree is defined by its level order traversal.

For example:

3 9 20 null null 15 7

outputFormat

Output the maximum depth of the binary tree on a single line to standard output (stdout).

For example, the output for the above input would be:

3
## sample
3 9 20 null null 15 7
3