import functools @functools.lru_cache() class Solution(object): def insertIntoBST(self, root, val): """ :type root: TreeNode :type val: int :rtype: TreeNode """ if not root: return if val > root.val: if not root.right: root.right = TreeNode(val) else: self.insertIntoBST(root.right, val) else: if not root.left: root.left = TreeNode(val) else: self.insertIntoBST(root.left, val) return root