← 返回 tesla 的题目列表Burning a Binary Tree (Time to Burn Entire Tree)
类型:online_judge
Given a binary tree, one node is set on fire at time t=0. Fire spreads each unit time from a burning node to its adjacent nodes (adjacent means parent or child).
Compute the time needed to burn the entire tree (i.e., the maximum number of time units until the last node catches fire).
You may assume all node values are unique.
The input provides the tree structure and the start node value.
Input (stdin)
Line 1: integer n, number of level-order tokens.
Line 2: n tokens in level-order; missing nodes are denoted by null (total tokens equals n).
Line 3: integer start, the value of the starting (burning) node.
Output (stdout)
Print one integer: time to burn the whole tree.
Constraints
1 <= n <= 2 * 10^5
Number of non-null nodes m >= 1
Node values fit in 32-bit signed int.
Example
Input
1
1
1
Output
0