代码随想录算法训练营第二十天 | 235. 二叉搜索树的最近公共祖先, 701.二叉搜索树中的插入操作 & 450.删除二叉搜索树中的节点
235. Lowest Common Ancestor of a Binary Search Tree
思路:因为之前做了二叉树的公共祖先,我就自己思考了一下如何利用二叉搜索树的特点,然后也想到了如果一个节点的值在p和q之间的话,可能就是p和q的公共祖先。但没有说服自己为什么一定是这样。看了视频之后发现从root开始找到的第一个值在p和q之间的节点就一定是最小公共祖先,卡哥的解释好妙
题目链接:235. Lowest Common Ancestor of a Binary Search Tree
文章讲解:二叉搜索树的最近公共祖先
视频讲解:二叉搜索树的最近公共祖先
我的代码:
- C++
- 递归
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if (!root) { return root; } if (root->val > p->val && root->val > q->val) { TreeNode* left = lowestCommonAncestor(root->left, p, q); if (left != NULL) { return left; } } if (root->val < p->val && root->val < q->val) { TreeNode* right = lowestCommonAncestor(root->right, p, q); if (right != NULL) { return right; } } return root; } };
- 递归
- Python
- 迭代法
# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': cur = root while cur: if cur.val > p.val and cur.val > q.val: cur = cur.left elif cur.val < p.val and cur.val < q.val: cur = cur.right else: return cur
- 迭代法
701. Insert into a Binary Search Tree
思路:刚开始看题的时候居然就想到了可以统一添加在叶子结点,然后用很奇怪的办法写出了迭代法,看了视频之后又写了一遍递归法。写递归还是有点不熟练诶
题目链接:701. Insert into a Binary Search Tree
文章讲解:二叉搜索树中的插入操作
视频讲解:二叉搜索树中的插入操作
我的代码:
- Python
- 奇怪的迭代法
# 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: Optional[TreeNode], val: int) -> Optional[TreeNode]: if not root: return TreeNode(val) cur, pre = root, root leftOrRight = True while cur: if cur.val > val: pre = cur cur = cur.left leftOrRight = True elif cur.val < val: pre = cur cur = cur.right leftOrRight = False if leftOrRight: pre.left = TreeNode(val) else: pre.right = TreeNode(val) return root
- 递归法
# 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: Optional[TreeNode], val: int) -> Optional[TreeNode]: if not root: return TreeNode(val) if root.val > val: root.left = self.insertIntoBST(root.left, val) elif root.val < val: root.right = self.insertIntoBST(root.right, val) return root
- 奇怪的迭代法
450. Delete Node in a BST
思路:直接看了视频讲解,卡尔学长讲的太清晰了,只要按照五种情况去考虑就可以很清楚的写出来:1.未找到key;2.左空右空;3.左不空右空;4.左空右不空;5.左不空右不空
题目链接:450. Delete Node in a BST
文章讲解:删除二叉搜索树中的节点
视频讲解:删除二叉搜索树中的节点
我的代码:
- Python
# 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]:
# 1.没找到key
if not root:
return None
if root.val == key:
# 2.左空右空 (叶子节点)
if not root.left and not root.right:
return None
# 3.左空右不空
elif not root.left and root.right != None:
return root.right
# 4.左不空右空
elif root.left != None and not root.right:
return root.left
# 5.左不空右不空
else:
cur = root.right
while cur.left:
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
总结
不确定第二遍刷的时候还会不会做,但是这次刷收获挺大的
继续加油赶进度ing