#K12921. Good Binary Tree Verification
Good Binary Tree Verification
Good Binary Tree Verification
You are given a binary tree where each node is represented by a triplet of integers (value, left, right)
. A value of -1
indicates a missing child. The tree is considered good if for every level of the tree, the sequence of node values (traversed from left to right) is strictly increasing. In other words, if a level has nodes with values \(a_1, a_2, \dots, a_k\), then the condition
\(a_1 < a_2 < \dots < a_k\)
must hold. If the tree is empty, it is considered good.
inputFormat
The input is read from stdin
and has the following format:
- The first line contains an integer
n
which is the number of nodes in the tree. Ifn = 0
, the tree is empty. - If
n > 0
, the followingn
lines each contain three space-separated integers:value
,left
, andright
. Here,left
andright
are the values of the left and right children respectively, with-1
indicating a missing child.
outputFormat
Output a single line to stdout
containing either YES
if the binary tree is good or NO
otherwise.
6
10 5 15
5 3 6
15 -1 20
3 -1 -1
6 -1 -1
20 -1 -1
YES