← 返回 google 的题目列表Longest Path With the Same Node Value in a Binary Tree
类型:online_judge
Problem: Longest Path With the Same Node Value in a Binary Tree
Given a binary tree. Although the interview described it as a BST, the BST property is not needed.
Find the longest path such that every node on the path has the same value.
The path may start and end at any nodes, but it must be connected through parent-child edges. Return the length of the path measured by the number of edges.
Input Format
The first line contains an integer n, the length of the level-order traversal array.
The second line contains n tokens representing the level-order traversal of the tree. A missing node is represented by null.
Output Format
Print one integer: the length of the longest same-value path in number of edges.
Constraints
0 <= n <= 10^5
Node values are 32-bit signed integers.
Example
Input:
5
4 4 5 1 1 null 5
Output:
2
Example
Input
5
4 4 5 1 1 null 5
Output
2