#K36872. Validate Binary Search Tree
Validate Binary Search Tree
Validate Binary Search Tree
You are given the level-order representation of a binary tree, where each node's value is an integer and the string "null" represents a missing node.
Your task is to determine whether this binary tree is a valid Binary Search Tree (BST). A BST is defined such that for every node with value \(v\):
- Every node in its left subtree has a value less than \(v\).
- Every node in its right subtree has a value greater than \(v\).
Formally, for each node, if we denote its allowed value range as \((lower, upper)\), then the node's value must satisfy \(lower < node.val < upper\) where the initial lower and upper bounds are \(-\infty\) and \(+\infty\), respectively.
Input is given as a single line containing comma-separated values representing the tree in level-order. For example, the input "2,1,3" represents the tree:
2 / \ 1 3
Your program should output either "True" or "False" on a single line to indicate whether the tree is a valid BST.
inputFormat
The input consists of a single line containing comma-separated tokens. Each token is either an integer or the string "null". These tokens represent the nodes of a binary tree in level-order traversal. For example:
2,1,3
represents the tree with root 2, left child 1, and right child 3.
outputFormat
Output a single line: True
if the given binary tree is a valid BST, otherwise False
.
2,1,3
True