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.

Stuck? AI Nudge Available

Get a conceptual hint to guide your logic without spoiling the final implementation.

03

Study the solution

The solution is waiting

Give it an honest attempt first — then compare your thinking with the full walkthrough.

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.