Insert Into BST

Given the root node of a valid BST and a value to insert into the tree, return a new root node representing the valid BST with the addition of the new item. If the new item already exists in the binary search tree, do not insert anything.

You must expand on the original BST by adding a leaf node. Do not change the structure of the original BST.


  • bst: a binary tree representing the existing BST.
  • val: an integer representing the value to be inserted.


A valid BST with the inserted number, or the same BST if the number already exists.


Example 1:


1tree = <See explanation>
2val = 7

Output: <See explanation>


Before insertion:

After insertion:

Try it yourself




Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

Contrary to popular belief, Lorem Ipsum is not simply random text.

1  >>> a = [1, 2, 3]
2  >>> a[-1]
3  3

Get premium for instant access to all content and solutions