Leetcode 701. Insert into a Binary Search Tree

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

猜你喜欢

转载自www.cnblogs.com/zywscq/p/10652724.html