Example: Insertion in BST - rFronteddu/general_wiki GitHub Wiki
public TreeNode insertIntoBST(TreeNode root, int val) {
// Base case: If the root is null, create a new node with the given value
if (root == null) {
return new TreeNode(val);
}
// If the value is less than the root's value, insert into the left subtree
if (val < root.val) {
root.left = insertIntoBST(root.left, val);
}
// If the value is greater than the root's value, insert into the right subtree
else {
root.right = insertIntoBST(root.right, val);
}
return root;
}