Imagine the following tree: [4,6,5,null,7,null,3,2,null]
-
-
The time complexity of inserting and removing from a BST is proportional to the height of the tree. This is because we are essentially running a binary search on the tree until we reach the target position to insert into, or the target node to remove.
O(log n) if the tree is balanced. However, if the tree is unbalanced, the time complexity can be O(n) in worst case.
The space complexity of these operations is O(log n) in the bestt case and O(n) in the worst case. This is because we are using recursion to traverse the tree, and the number of recursive calls is proportionalto the height of the tree.