#C11790. Determine if a Binary Tree is a Binary Search Tree (BST)
Determine if a Binary Tree is a Binary Search Tree (BST)
Determine if a Binary Tree is a Binary Search Tree (BST)
You are given a binary tree in level-order traversal format. The tree is represented as a sequence of tokens separated by spaces. Each token is either an integer (representing the node's value) or the string null
(representing a missing node). Your task is to determine whether the given binary tree satisfies the properties of a Binary Search Tree (BST).
A BST must satisfy the following condition for every node:
- All nodes in its left subtree have values strictly less than the node's value.
- All nodes in its right subtree have values strictly greater than the node's value.
If the binary tree is empty, it should be considered as a valid BST.
Note: The input is provided via standard input (stdin) and the result should be printed to standard output (stdout) as either True
or False
.
Tree Construction Hint: Interpret the input tokens as the level order traversal of the tree. For example, the input 10 5 15 2 7 12 18 represents the following tree:
10 / \ 5 15 / \ / \ 2 7 12 18
inputFormat
The input is a single line containing a space-separated list of tokens representing the level-order traversal of a binary tree. Each token is either an integer or the string null
. An empty input indicates an empty tree.
outputFormat
Output a single line to stdout: True
if the binary tree is a valid BST, or False
if it is not.
True