Question Bank
/

Binary Search Tree Basics

Binary Search Tree Basics

Quick drills on the BST ordering invariant, in-order traversal yielding sorted output, and the most common search and insert operations.

Question Bank
Easy
JavaScript
4 questions
bst
binary-tree
data-structures
fundamentals

691 views

13

State the BST ordering invariant in one sentence, then check whether the tree built below satisfies it. If not, name the offending node.

Examples

Example 1:

Input: tree 10 -> (5 -> (2, 7), 15 -> (12, 20))
Output: valid BST; in-order traversal = [2, 5, 7, 10, 12, 15, 20]
Explanation: For every node, all values in the left subtree are strictly less and all values in the right subtree are strictly greater. The sorted in-order traversal confirms the invariant.
class Node {
    constructor(value, left = null, right = null) {
        this.value = value;
        this.left = left;
        this.right = right;
    }
}
const root = new Node(10,
    new Node(5, new Node(2), new Node(7)),
    new Node(15, new Node(12), new Node(20))
);