235.二叉搜索树的最近公共祖先
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def traversal(self,cur,p,q):
if cur is None:
return cur
if cur.val>p.val and cur.val>q.val:#往左
left=self.traversal(cur.left,p,q)
if left is not None:
return left
if cur.val<p.val and cur.val<q.val:
right=self.traversal(cur.right,p,q)#搜索整棵树要有一判断个变量接住才
if right is not None:
return right
return cur
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
return self.traversal(root,p,q)
迭代法
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
#迭代法
while root:
if root.val>p.val and root.val>q.val:
root=root.left
elif root.val<p.val and root.val<q.val:
root=root.right
else:
return root
return None
701.二叉搜索树中的插入操作
如何通过递归函数返回值完成了新加入节点的父子关系赋值操作了,下一层将加入节点返回,本层用root->left或者root->right将其接住。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def __init__(self):
self.parent=None
def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
self.parent=TreeNode(0)
if root is None:
return TreeNode(val)
self.traversal(root,val)
return root
def traversal(self,cur,val):
if cur is None:
node=TreeNode(val)
if val >self.parent.val:
self.parent.right=node#parent前加个self
else:
self.parent.left=node
return
self.parent=cur
if cur.val>val:
self.traversal(cur.left,val)#加完后逐层返回
if cur.val<val:
self.traversal(cur.right,val)
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def insertIntoBST(self,root,val):
if root is None:
node=TreeNode(val)
return node
cur=root
parent=root#记录上一个节点,用于连接新节点
while cur is not None:
parent=cur#最后满足时cur指向当前值,parent指向父节点
if cur.val>val:
cur=cur.left
else:
cur=cur.right
node=TreeNode(val)
if val<parent.val:
parent.left=node#将新节点连接到父节点的左子树
else:
parent.right=node
return root
450.删除二叉搜索树中的节点
有以下五种情况:
- 第一种情况:没找到删除的节点,遍历到空节点直接返回了
- 找到删除的节点
- 第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点
- 第三种情况:删除节点的左孩子为空,右孩子不为空,删除节点,右孩子补位,返回右孩子为根节点
- 第四种情况:删除节点的右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点
- 第五种情况:左右孩子节点都不为空,则将删除节点的左子树头结点(左孩子)放到删除节点的右子树的最左面节点的左孩子上,返回删除节点右孩子为新的根节点。
- 因为二叉搜索树添加节点只需要在叶子上添加就可以的,不涉及到结构的调整,而删除节点操作涉及到结构的调整。
-
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]: if root is None: return root if root.val==key: if root.left is None and root.right is None: return None elif root.left is None: return root.right elif root.right is None: return root.left else:#第五种情况需要重构二叉树 cur=root.right while cur.left is not None: cur=cur.left#找到最小的值,右节点的最左 cur.left=root.left return root.right if root.val>key: root.left=self.deleteNode(root.left,key)#遍历整颗树 if root.val<key: root.right=self.deleteNode(root.right,key) return root