23 · Binary Search Trees

#701 Insert into a Binary Search Tree

Mediumsolve on LeetCode ↗

frequently reported atMicrosoftAmazonGoogleLinkedInBloomberg

the problem

In plain words

Insert val (guaranteed not already present) into the BST and return the root. Any valid BST is accepted.

in root = [4,2,7,1,3], val = 5

out [4,2,7,1,3,5]

5 > 4 → right; 5 < 7 → left of 7, which is empty.

in root = [], val = 5

out [5]

  • 0 ≤ number of nodes ≤ 10⁴
  • -10⁸ ≤ Node.val, val ≤ 10⁸
  • All values unique; val is new

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #700
    2. #450
    3. #108