Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert the value into the BST. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST. No…
class Solution: def insertIntoBST(self, root, val): """ Time: O(log(n)) [average case] Space: O(1) """ new_node = TreeNode(val) if not root: return new_node curr = root while True: if curr.val > val: if not curr.left: curr…