1 class Solution: 2 3 def insertIntoBST(self, root, val): 4 """ 5 Time: O(log(n)) [average case] 6 Space: O(1) 7 """ 8 new_node = TreeNode(val) 9 10 if not root: 11 return new_node 12 13 curr = root 14 while True: 15 if curr.val > val: 16 if not curr.left: 17 curr.left = new_node 18 break 19 else: 20 curr = curr.left 21 else: 22 if not curr.right: 23 curr.right = new_node 24 break 25 else: 26 curr = curr.right 27 return root