You are given the root of a Binary Search Tree (BST) and an integer key.
A Binary Search Tree (BST) follows these rules:
Input: root = [4,2,7,1,3], key = 2
Output: [4,2,7,1,3]
Explanation: The key already exists in the BST, so the tree remains unchanged.
Input: root = [4,2,7,1,3], key = 5
Output: [4,2,7,1,3,5]
Explanation: The key 5 is inserted while maintaining the BST property.
Input: root = [], key = 8
Output: [8]
Explanation: Since the BST is empty, the new node becomes the root.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
50 30 70 20 40 60 80 25
50 30 70 20 40 60 80 25
40 20 60 10 30 50 70 55
40 20 60 10 30 50 70 55
80 50 100 30 60 90 120 75
80 50 100 30 60 90 120 75