← 返回 google 的题目列表Longest Consecutive Path in a Binary Tree
类型:online_judge
Problem
Given a binary tree, return the length of the longest consecutive increasing path.
Path requirements:
The path must go from parent to child;
For every adjacent pair u -> v, v.val = u.val + 1 must hold;
The path can start at any node, not necessarily the root.
Follow-up: If the path can extend in any direction, meaning it may go from a child to its parent and then to another child, how would you modify the solution?
Input Format
One line representing the level-order traversal of the binary tree. Empty nodes are represented by null.
Output Format
Print one integer, the length of the longest consecutive increasing path.
Constraints
1 <= number of nodes <= 10^5
-10^9 <= Node.val <= 10^9
Example
Input:
1 2 null 3 null 4 null
Output:
4
Explanation: The longest path is 1 -> 2 -> 3 -> 4.
Example
Input
1 2 null 3 null 4 null
Output
4