701. Insert into a Binary Search Tree

Practice Insert into a Binary Search Tree for free on NudgeCode: Medium, Binary Tree pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.

Insert val into a binary search tree and return the root. val is not already in the tree.

Use the standard insertion: starting at the root, go left when val is smaller than the current node and right when it is larger, and attach val as a new leaf where you fall off the tree. Do not rearrange existing nodes, so the answer is unique.

Example

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

Constraints