Skip to solution
mediumDSA

What is a binary search tree (BST)?

675 views
01

Understand the problem

Explain BST properties and operations.

treesbst
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

A BST is a binary tree where every node's left subtree holds smaller keys and right subtree larger keys. This gives O(log n) search/insert/delete when balanced, but degrades to O(n) if it becomes skewed — which self-balancing trees (AVL, Red-Black) prevent.

Solution ready — 2 min read

Classified // press E to declassify

04

Read the code

BST insert + search
Run Playground
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left = self.right = None

def insert(root, val):
    if not root:
        return TreeNode(val)
    if val < root.val:
        root.left = insert(root.left, val)
    else:
        root.right = insert(root.right, val)
    return root

def search(root, val):
    while root:
        if val == root.val: return True
        root = root.left if val < root.val else root.right
    return False


# --- demo ---
root = None
for v in [8, 4, 12, 2, 7, 14]:
    root = insert(root, v)
print(search(root, 7))    # True
print(search(root, 5))    # False
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 58 of 127 decoded in the Data Structures & Algorithms track. One more won't hurt.

Back to track