
Problem Statement
In this problem, you need to insert a given value into a Binary Search Tree (BST) that does not previously contain this value. After insertion, the tree must still satisfy BST properties:
- The left subtree of a node contains only nodes with values less than the node’s value.
- The right subtree only contains values greater than the node’s value.
The problem guarantees that the value to be inserted does not already exist in the tree. Any valid BST configuration after insertion is acceptable.
Examples
Example 1
Input:
root = [4,2,7,1,3], val = 5
Output:
[4,2,7,1,3,5]
Explanation:
One possible BST is:
4
/ \
2 7
/ \ /
1 3 5Example 2
Input:
root = [40,20,60,10,30,50,70], val = 25
Output:
[40,20,60,10,30,50,70,null,null,25]
Explanation:
25 is inserted as the left child of 30:
40
/ \
20 60
/ \ / \
10 30 50 70
/
25Example 3
Input:
root = [4,2,7,1,3,null,null,null,null,null,null], val = 5
Output:
[4,2,7,1,3,5]
Explanation:
5 is added as the left child of 7.
Constraints
- The number of nodes in the tree is in the range
[0, 10^4]. -10^8 <= Node.val <= 10^8- All
Node.valvalues are unique. -10^8 <= val <= 10^8valdoes not exist in the original BST.
Approach and Intuition
To insert a value into a BST and preserve its structure:
Base Case:
- If the root is
None, return a new node with the value. This becomes the root.
- If the root is
Recursive Traversal:
Start from the root and compare the value to insert:
- If
val < root.val, recurse into the left subtree. - If
val > root.val, recurse into the right subtree.
- If
Once a
Noneposition is found in the correct direction, insert the new node there.
Return the Root:
- After insertion, return the (possibly unchanged) root node.
This strategy ensures BST integrity and works in O(h) time, where h is the height of the tree. Since multiple valid configurations are acceptable, there’s no need to balance or reshape the tree.
This method handles all edge cases, including inserting into an empty tree or adding to any depth level of the existing tree.
Solutions
- Java
class Solution {
public TreeNode addValueToBST(TreeNode root, int value) {
TreeNode current = root;
while (current != null) {
if (value > current.val) {
if (current.right == null) {
current.right = new TreeNode(value);
return root;
}
else current = current.right;
}
else {
if (current.left == null) {
current.left = new TreeNode(value);
return root;
}
else current = current.left;
}
}
return new TreeNode(value);
}
}
The task focuses on inserting a new value into a Binary Search Tree (BST) using Java. The provided solution defines a method addValueToBST inside a class named Solution.
- The method accepts a
TreeNoderepresenting the root of the BST, and an integervaluewhich is the new value to be inserted. - A
TreeNodeis maintained to traverse the BST. The traversal starts from the root. - As you move through the tree:
- If the
valueis greater than the current node's value, you proceed to the right child. - If the right child does not exist, a new
TreeNodewith the newvalueis created and becomes the right child, then the original root of the tree is returned. - If the value is less than the current node's value, the procedure is similar but towards the left child.
- If the
- If the insertion point is located (where the current node does not have the appropriate child), a new node with the
valueis created in the correct position. - If the root is
null(i.e., the tree is empty), a newTreeNodewith the givenvalueis created and returned as the new root of the tree.
This implementation retains the properties of a BST, where all nodes in the left subtree are less than the node's value and all in the right are greater, updating the tree structure only where necessary and maintaining overall efficiency.