Reqflow
Searching·Intermediate

BST: Insert

Time O(h)Space O(h)
·

BST insert follows the same comparison logic as search but instead of returning when it finds the value, it follows the path until it hits a null child and inserts a new leaf node there. The BST property — left subtree smaller, right subtree larger — is automatically maintained. In a balanced tree this is O(log n).

Example: Insert 6 into the BST.

When to use this

See also

← All algorithms