BST Insert & Search
Treesmedium#14Insert values maintaining BST property — left smaller, right larger
Ready
Step 1 of 0
Speed
Input Data
Comma-separated numbers (0-999). Min 2 values.
Algorithm Code
1function bstInsert(root, value) {2 if (!root) return { value, left: null, right: null };3 if (value < root.value) {4 root.left = bstInsert(root.left, value);5 } else {6 root.right = bstInsert(root.right, value);7 }8 return root;9}
Best
O(log n)
Average
O(log n)
Worst
O(n)
Space
O(n)
About BST Insert & Search
A Binary Search Tree maintains the property that for every node, all values in the left subtree are smaller and all values in the right subtree are larger. Insert and search operations follow this property to navigate the tree efficiently. Worst case (skewed tree) degrades to O(n).
Time Complexity: Best: O(log n), Average: O(log n), Worst: O(n). Space: O(n).