Day22(10.12)

235 二叉搜索树的最近公共祖先

class Solution:
    def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
        ret=root
        while True:
            if ret.valp.val and ret.val >q.val:
                ret=ret.left
            else:
                break
        return ret

701 二叉搜索树中的插入操作

class Solution:
    def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
        if not root:
            return TreeNode(val)
        pos=root
        while pos:
            if pos.val

450 删除二叉搜索树中的节点

class Solution:
    def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
        if root is None:
            return None
        if root.val>key:
            root.left=self.deleteNode(root.left,key)
        elif root.val

你可能感兴趣的:(Day22(10.12))