Trees · Coding problem

Search in a BST

A free coding problem from the BST Search and Insert skill. Read it here, then solve it in the app in Java or Python.

  • Easy
  • Coding problem
  • Free
  • Java and Python

The problem

Given the root of a binary search tree and a value, return whether a node with that value exists in the tree.

Input format

<tree in heap-array format>
val

Example

Input:
7
5 3 8 2 4 7 9
4

Output: true

Constraints

  • 0 <= number of nodes <= 5000
  • -10^4 <= node value, val <= 10^4
  • All node values are distinct.

How it's graded

  • Run checks your code against the examples. Submit also runs a hidden suite in four groups - basic cases, edge cases, random inputs and performance tests at the size limit - and shows which groups pass, never the hidden inputs.
  • Stuck? Hints come in four steps, from a nudge to a code skeleton, each costing part of the points.
  • Once you pass, you see the approaches from brute force to optimal, and where your solution lands among them.

More problems in BST Search and Insert

Solve it Search in a BST free

No card needed. Lessons, practice questions and easy problems are free forever.

Free plan, no card · 7-day Pro trial · Cancel anytime