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

 

posted @ 2019-04-04 06:18  周洋  阅读(182)  评论(0编辑  收藏  举报