#K72652. Longest Increasing Path in a Binary Tree
Longest Increasing Path in a Binary Tree
Longest Increasing Path in a Binary Tree
You are given a binary tree. Each node contains an integer value. The tree is represented in level-order and missing nodes are denoted by null
. Starting from the root, you need to determine the length of the longest strictly increasing path, where the path length is defined as the number of edges. In other words, if you can travel from the root to a child and that child’s value is greater than its parent, you extend the path by one edge.
Formally, if the path is represented as $$a_0,a_1,a_2,\dots,a_k$$, then for each \(1 \le i \le k\), we require \(a_i > a_{i-1}\). The answer is \(k\) (which is the number of edges) or 0 if no increasing edge exists.
Note: The input is provided via standard input and the output must be printed to standard output.
inputFormat
The input is a single line containing the level-order traversal of the binary tree. The node values are separated by commas. Use null
to denote missing nodes. For example:
1,2,3
represents the binary tree:
1 / \ 2 3
outputFormat
Output a single integer representing the number of edges in the longest strictly increasing path starting from the root.
## sample1,2,3
1