#K93517. Shady Tree
Shady Tree
Shady Tree
Given a binary tree represented in level order, determine whether it is a shady tree or not.
A binary tree is called a shady tree if for every node (with a non-missing value), the absolute difference between the values of its left child and right child is at most k. In mathematical notation, for every valid node index i, let its left child value be L and right child value be R (if a child is missing or its value is -1, consider its value as 0). The tree is shady if:
\( |L - R| \leq k \)
A single node tree is always considered shady.
The tree is given in level order where a value of -1 indicates a missing node.
inputFormat
The first line contains two space-separated integers n and k, where n is the number of nodes, and k is the allowed maximum absolute difference between the values of the left and right children for each node.
The second line contains n space-separated integers representing the nodes of the binary tree in level order. A value of -1 represents that the node is missing.
outputFormat
Output a single line containing either YES
if the tree is a shady tree, or NO
otherwise.
7 2
1 3 5 -1 -1 6 7
YES