#C7135. Valid BST Verification
Valid BST Verification
Valid BST Verification
You are given the level order representation of a binary tree. Your task is to determine whether the binary tree is a valid Binary Search Tree (BST). In a BST, for every node with value \(v\), all nodes in its left subtree must have values strictly less than \(v\), and all nodes in its right subtree must have values strictly greater than \(v\). Duplicate values are not allowed.
Input Format: The tree is given in a single line containing space-separated tokens representing the level order traversal of the tree. Each token is either an integer or the string null
representing a missing node.
Output Format: Print True
if the tree is a valid BST or False
otherwise.
Example:
For the input:
2 1 3
The BST is valid so the output should be:
True
The BST condition can be written in \(\LaTeX\) as:
\(\forall \text{ node } v, \; \forall u \in L_v, \; u v\)
inputFormat
The input is read from standard input (stdin) and consists of a single line containing the level order traversal of the binary tree. Use the keyword null
(without quotes) to denote missing nodes.
For example:
5 1 4 null null 3 6
outputFormat
Output to standard output (stdout) a single line: True
if the tree is a valid BST, or False
otherwise.
2 1 3
True
</p>