本节内容
- 235. 二叉搜索树的最近公共祖先
- 701.二叉搜索树中的插入操作
- 450.删除二叉搜索树中的节点
235. 二叉搜索树的最近公共祖先※
建议:相对于 二叉树的最近公共祖先 本题就简单一些了,因为 可以利用二叉搜索树的特性。
题目链接: https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-search-tree/
文章讲解: https://programmercarl.com/0235.%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E7%9A%84%E6%9C%80%E8%BF%91%E5%85%AC%E5%85%B1%E7%A5%96%E5%85%88.html
视频讲解: https://www.bilibili.com/video/BV1Zt4y1F7ww
题目分析


方案一
不考虑其是二叉搜索树
此方法可以参考: 236. 二叉树的最近公共祖先
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null || root.val == p.val || root.val == q.val) return root;
TreeNode treeNodeLeft = lowestCommonAncestor(root.left, p, q); TreeNode treeNodeRight = lowestCommonAncestor(root.right, p, q);
if (treeNodeLeft == null && treeNodeRight == null) return null; else if (treeNodeRight == null) return treeNodeLeft; else if (treeNodeLeft == null) return treeNodeRight; else return root; } }
|
结果
解答成功:
执行耗时:6 ms,击败了34.02% 的Java用户
内存消耗:42.9 MB,击败了17.60% 的Java用户
方案二
考虑其是二叉搜索树
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null) return null;
if (root.val > p.val && root.val > q.val){ return lowestCommonAncestor(root.left, p, q); }
if (root.val < p.val && root.val < q.val){ return lowestCommonAncestor(root.right, p, q); }
return root;
} }
|
结果
解答成功:
执行耗时:6 ms,击败了34.02% 的Java用户
内存消耗:42.9 MB,击败了23.42% 的Java用户
代码随想录
https://programmercarl.com/0235.%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E7%9A%84%E6%9C%80%E8%BF%91%E5%85%AC%E5%85%B1%E7%A5%96%E5%85%88.html
思路
本题是二叉搜索树,二叉搜索树是有序的,那得好好利用一下这个特点。
在有序树里,如果判断一个节点的左子树里有p,右子树里有q呢?
因为是有序树,所有 如果 中间节点是 q 和 p 的公共祖先,那么 中节点的数组 一定是在 [p, q]区间的。即 中节点 > p && 中节点 < q 或者 中节点 > q && 中节点 < p。
那么只要从上到下去遍历,遇到 cur节点是数值在[p, q]区间中则一定可以说明该节点cur就是q 和 p的公共祖先。 那问题来了,一定是最近公共祖先吗?
如图,我们从根节点搜索,第一次遇到 cur节点是数值在[p, q]区间中,即 节点5,此时可以说明 p 和 q 一定分别存在于 节点 5的左子树,和右子树中。

此时节点5是不是最近公共祖先? 如果 从节点5继续向左遍历,那么将错过成为q的祖先, 如果从节点5继续向右遍历则错过成为p的祖先。
所以当我们从上向下去递归遍历,第一次遇到 cur节点是数值在[p, q]区间中,那么cur就是 p和q的最近公共祖先。
理解这一点,本题就很好解了。
而递归遍历顺序,本题就不涉及到 前中后序了(这里没有中节点的处理逻辑,遍历顺序无所谓了)。
如图所示:p为节点6,q为节点9

可以看出直接按照指定的方向,就可以找到节点8,为最近公共祖先,而且不需要遍历整棵树,找到结果直接返回!
递归法
递归三部曲如下:
参数就是当前节点,以及两个结点 p、q。
返回值是要返回最近公共祖先,所以是TreeNode * 。
代码如下:
1
| TreeNode* traversal(TreeNode* cur, TreeNode* p, TreeNode* q)
|
遇到空返回就可以了,代码如下:
1
| if (cur == NULL) return cur;
|
其实都不需要这个终止条件,因为题目中说了p、q 为不同节点且均存在于给定的二叉搜索树中。也就是说一定会找到公共祖先的,所以并不存在遇到空的情况。
在遍历二叉搜索树的时候就是寻找区间[p->val, q->val](注意这里是左闭又闭)
那么如果 cur->val 大于 p->val,同时 cur->val 大于q->val,那么就应该向左遍历(说明目标区间在左子树上)。
需要注意的是此时不知道p和q谁大,所以两个都要判断
代码如下:
1 2 3 4 5 6
| if (cur->val > p->val && cur->val > q->val) { TreeNode* left = traversal(cur->left, p, q); if (left != NULL) { return left; } }
|
细心的同学会发现,在这里调用递归函数的地方,把递归函数的返回值left,直接return。
如果递归函数有返回值,如何区分要搜索一条边,还是搜索整个树。
搜索一条边的写法:
1 2
| if (递归函数(root->left)) return ; if (递归函数(root->right)) return ;
|
搜索整个树写法:
1 2 3
| left = 递归函数(root->left); right = 递归函数(root->right); left与right的逻辑处理;
|
本题就是标准的搜索一条边的写法,遇到递归函数的返回值,如果不为空,立刻返回。
如果 cur->val 小于 p->val,同时 cur->val 小于 q->val,那么就应该向右遍历(目标区间在右子树)。
1 2 3 4 5 6
| if (cur->val < p->val && cur->val < q->val) { TreeNode* right = traversal(cur->right, p, q); if (right != NULL) { return right; } }
|
剩下的情况,就是cur节点在区间(p->val <= cur->val && cur->val <= q->val)或者 (q->val <= cur->val && cur->val <= p->val)中,那么cur就是最近公共祖先了,直接返回cur。
代码如下:
代码实现
1 2 3 4 5 6 7
| class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (root.val > p.val && root.val > q.val) return lowestCommonAncestor(root.left, p, q); if (root.val < p.val && root.val < q.val) return lowestCommonAncestor(root.right, p, q); return root; } }
|
迭代法
利用其有序性,迭代的方式还是比较简单的,解题思路在递归中已经分析了。
代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { while (true) { if (root.val > p.val && root.val > q.val) { root = root.left; } else if (root.val < p.val && root.val < q.val) { root = root.right; } else { break; } } return root; } }
|
701.二叉搜索树中的插入操作※
建议:本题比想象中的简单,大家可以先自己想一想应该怎么做,然后看视频讲解,就发现 本题为什么比较简单了。
题目链接: https://leetcode.cn/problems/insert-into-a-binary-search-tree/
文章讲解: https://programmercarl.com/0701.%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E4%B8%AD%E7%9A%84%E6%8F%92%E5%85%A5%E6%93%8D%E4%BD%9C.html
视频讲解: https://www.bilibili.com/video/BV1Et4y1c78Y
题目分析



方案一
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| class Solution { public TreeNode insertIntoBST(TreeNode root, int val) { if (root == null) { TreeNode node = new TreeNode(val); return node; } TreeNode treeNode = null; if (root.val > val) { treeNode = insertIntoBST(root.left, val); if (root.left != treeNode) { root.left = treeNode; } } else { treeNode = insertIntoBST(root.right, val); if (root.right != treeNode) { root.right = treeNode; } } return root; } }
|
结果
解答成功:
执行耗时:0 ms,击败了100.00% 的Java用户
内存消耗:42.6 MB,击败了57.55% 的Java用户
代码随想录
https://programmercarl.com/0701.%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E4%B8%AD%E7%9A%84%E6%8F%92%E5%85%A5%E6%93%8D%E4%BD%9C.html
思路
这道题目其实是一道简单题目,但是题目中的提示:有多种有效的插入方式,还可以重构二叉搜索树,一下子吓退了不少人,瞬间感觉题目复杂了很多。
其实可以不考虑题目中提示所说的改变树的结构的插入方式。
如下演示中可以看出:只要按照二叉搜索树的规则去遍历,遇到空节点就插入节点就可以了。

例如插入元素10 ,需要找到末尾节点插入便可,一样的道理来插入元素15,插入元素0,插入元素6,需要调整二叉树的结构么? 并不需要。。
只要遍历二叉搜索树,找到空节点 插入元素就可以了,那么这道题其实就简单了。
接下来就是遍历二叉搜索树的过程了。
递归
递归三部曲:
参数就是根节点指针,以及要插入元素,这里递归函数要不要有返回值呢?
可以有,也可以没有,但递归函数如果没有返回值的话,实现是比较麻烦的,下面也会给出其具体实现代码。
有返回值的话,可以利用返回值完成新加入的节点与其父节点的赋值操作。(下面会进一步解释)
递归函数的返回类型为节点类型TreeNode * 。
代码如下:
1
| TreeNode* insertIntoBST(TreeNode* root, int val)
|
终止条件就是找到遍历的节点为null的时候,就是要插入节点的位置了,并把插入的节点返回。
代码如下:
1 2 3 4
| if (root == NULL) { TreeNode* node = new TreeNode(val); return node; }
|
这里把添加的节点返回给上一层,就完成了父子节点的赋值操作了,详细再往下看。
此时要明确,需要遍历整棵树么?
别忘了这是搜索树,遍历整棵搜索树简直是对搜索树的侮辱,哈哈。
搜索树是有方向了,可以根据插入元素的数值,决定递归方向。
代码如下:
1 2 3
| if (root->val > val) root->left = insertIntoBST(root->left, val); if (root->val < val) root->right = insertIntoBST(root->right, val); return root;
|
到这里,大家应该能感受到,如何通过递归函数返回值完成了新加入节点的父子关系赋值操作了,下一层将加入节点返回,本层用root->left或者root->right将其接住。
整体代码如下:
1 2 3 4 5 6 7 8 9 10 11 12
| class Solution { public: TreeNode* insertIntoBST(TreeNode* root, int val) { if (root == NULL) { TreeNode* node = new TreeNode(val); return node; } if (root->val > val) root->left = insertIntoBST(root->left, val); if (root->val < val) root->right = insertIntoBST(root->right, val); return root; } };
|
可以看出代码并不复杂。
刚刚说了递归函数不用返回值也可以,找到插入的节点位置,直接让其父节点指向插入节点,结束递归,也是可以的。
那么递归函数定义如下:
1 2
| TreeNode* parent; void traversal(TreeNode* cur, int val)
|
没有返回值,需要记录上一个节点(parent),遇到空节点了,就让parent左孩子或者右孩子指向新插入的节点。然后结束递归。
代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13
| class Solution { public TreeNode insertIntoBST(TreeNode root, int val) { if (root == null) return new TreeNode(val); if (root.val < val){ root.right = insertIntoBST(root.right, val); }else if (root.val > val){ root.left = insertIntoBST(root.left, val); } return root; } }
|
迭代
在迭代法遍历的过程中,需要记录一下当前遍历的节点的父节点,这样才能做插入节点的操作。
代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| class Solution { public TreeNode insertIntoBST(TreeNode root, int val) { if (root == null) return new TreeNode(val); TreeNode newRoot = root; TreeNode pre = root; while (root != null) { pre = root; if (root.val > val) { root = root.left; } else if (root.val < val) { root = root.right; } } if (pre.val > val) { pre.left = new TreeNode(val); } else { pre.right = new TreeNode(val); }
return newRoot; } }
|
450.删除二叉搜索树中的节点※
建议:相对于 插入操作,本题就有难度了,涉及到改树的结构
题目链接: https://leetcode.cn/problems/delete-node-in-a-bst/
文章讲解: https://programmercarl.com/0450.%E5%88%A0%E9%99%A4%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B9.html
视频讲解: https://www.bilibili.com/video/BV1tP41177us
题目分析



方案一
剪不断,理还乱
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| class Solution { public TreeNode deleteNode(TreeNode root, int key) {
if (root == null || root.val == key) { return null; } TreeNode treeNode = null; if (root.val > key) { treeNode = deleteNode(root.left, key); if (treeNode == null) {
if (root.left == null) {
} else if (root.left.right == null) { root.left = root.left.left; } else { TreeNode nodeTihuanBrfor = root.left; TreeNode nodeTihuan = root.left.right;
if (nodeTihuan.left == null) { nodeTihuan.left = nodeTihuanBrfor.left; root.left = nodeTihuan; } else { while (nodeTihuan.left != null) { nodeTihuanBrfor = nodeTihuan; nodeTihuan = nodeTihuan.left; }
nodeTihuanBrfor.left = nodeTihuan.right; nodeTihuan.left = root.left.left; nodeTihuan.right = root.left.right; root.left = nodeTihuan; } } } } else { treeNode = deleteNode(root.right, key); if (treeNode == null) {
if (root.right == null) {
} else if (root.right.right == null) { root.right = root.right.left; } else { TreeNode nodeTihuanBrfor = root.right; TreeNode nodeTihuan = root.right.right;
if (nodeTihuan.left == null) { nodeTihuan.left = nodeTihuanBrfor.left; root.right = nodeTihuan; } else { while (nodeTihuan.left != null) { nodeTihuanBrfor = nodeTihuan; nodeTihuan = nodeTihuan.left; }
nodeTihuanBrfor.left = nodeTihuan.right; nodeTihuan.left = root.right.left; nodeTihuan.right = root.right.right; root.right = nodeTihuan; } } } }
return root; } }
|
结果
解答错误
77 / 92 个通过测试用例
方案二
查看代码随想录解决
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| class Solution { public TreeNode deleteNode(TreeNode root, int key) {
if (root == null) return null; if (root.val == key) { if (root.left == null) { return root.right; } else if (root.right == null) { return root.left; } else { TreeNode node = root.right; while (node.left != null) { node = node.left; } node.left = root.left; root = root.right; return root; } }
if (root.val > key) root.left = deleteNode(root.left, key); if (root.val < key) root.right = deleteNode(root.right, key); return root; } }
|
结果
解答成功:
执行耗时:0 ms,击败了100.00% 的Java用户
内存消耗:43.2 MB,击败了69.82% 的Java用户
代码随想录
https://programmercarl.com/0450.%E5%88%A0%E9%99%A4%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B9.html
思路
搜索树的节点删除要比节点增加复杂的多,有很多情况需要考虑。
递归
递归三部曲:
通过递归返回值删除节点。
代码如下:
1
| TreeNode* deleteNode(TreeNode* root, int key)
|
遇到空返回,其实这也说明没找到删除的节点,遍历到空节点直接返回了
1
| if (root == nullptr) return root;
|
这里就把二叉搜索树中删除节点遇到的情况都搞清楚。
有以下五种情况:
- 第一种情况:没找到删除的节点,遍历到空节点直接返回了
- 找到删除的节点
- 第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点
- 第三种情况:删除节点的左孩子为空,右孩子不为空,删除节点,右孩子补位,返回右孩子为根节点
- 第四种情况:删除节点的右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点
- 第五种情况:左右孩子节点都不为空,则将删除节点的左子树头结点(左孩子)放到删除节点的右子树的最左面节点的左孩子上,返回删除节点右孩子为新的根节点。
第五种情况有点难以理解,看下面动画:

动画中的二叉搜索树中,删除元素7, 那么删除节点(元素7)的左孩子就是5,删除节点(元素7)的右子树的最左面节点是元素8。
将删除节点(元素7)的左孩子放到删除节点(元素7)的右子树的最左面节点(元素8)的左孩子上,就是把5为根节点的子树移到了8的左孩子的位置。
要删除的节点(元素7)的右孩子(元素9)为新的根节点。.
这样就完成删除元素7的逻辑,最好动手画一个图,尝试删除一个节点试试。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| if (root->val == key) { if (root->left == nullptr) return root->right; else if (root->right == nullptr) return root->left; else { TreeNode* cur = root->right; while(cur->left != nullptr) { cur = cur->left; } cur->left = root->left; TreeNode* tmp = root; root = root->right; delete tmp; return root; } }
|
这里相当于把新的节点返回给上一层,上一层就要用 root->left 或者 root->right接住,代码如下:
1 2 3
| if (root->val > key) root->left = deleteNode(root->left, key); if (root->val < key) root->right = deleteNode(root->right, key); return root;
|
代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| class Solution { public TreeNode deleteNode(TreeNode root, int key) { if (root == null) return root; if (root.val == key) { if (root.left == null) { return root.right; } else if (root.right == null) { return root.left; } else { TreeNode cur = root.right; while (cur.left != null) { cur = cur.left; } cur.left = root.left; root = root.right; return root; } } if (root.val > key) root.left = deleteNode(root.left, key); if (root.val < key) root.right = deleteNode(root.right, key); return root; } }
|
普通二叉树的删除方式
这里我在介绍一种通用的删除,普通二叉树的删除方式(没有使用搜索树的特性,遍历整棵树),用交换值的操作来删除目标节点。
代码中目标节点(要删除的节点)被操作了两次:
- 第一次是和目标节点的右子树最左面节点交换。
- 第二次直接被NULL覆盖了。
思路有点绕,感兴趣的同学可以画图自己理解一下。
代码如下:(关键部分已经注释)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| class Solution { public: TreeNode* deleteNode(TreeNode* root, int key) { if (root == nullptr) return root; if (root->val == key) { if (root->right == nullptr) { return root->left; } TreeNode *cur = root->right; while (cur->left) { cur = cur->left; } swap(root->val, cur->val); } root->left = deleteNode(root->left, key); root->right = deleteNode(root->right, key); return root; } };
|
这个代码是简短一些,思路也巧妙,但是不太好想,实操性不强,推荐第一种写法
代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| class Solution { public TreeNode deleteNode(TreeNode root, int key) { root = delete(root,key); return root; }
private TreeNode delete(TreeNode root, int key) { if (root == null) return null;
if (root.val > key) { root.left = delete(root.left,key); } else if (root.val < key) { root.right = delete(root.right,key); } else { if (root.left == null) return root.right; if (root.right == null) return root.left; TreeNode tmp = root.right; while (tmp.left != null) { tmp = tmp.left; } root.val = tmp.val; root.right = delete(root.right,tmp.val); } return root; } }
|
迭代法
删除节点的迭代法还是复杂一些的,但其本质我在递归法里都介绍了,最关键就是删除节点的操作(动画模拟的过程)
代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| class Solution { public TreeNode deleteNode(TreeNode root, int key) { if (root == null){ return null; } TreeNode cur = root; TreeNode pre = null; while (cur != null){ if (cur.val < key){ pre = cur; cur = cur.right; } else if (cur.val > key) { pre = cur; cur = cur.left; }else { break; } } if (pre == null){ return deleteOneNode(cur); } if (pre.left !=null && pre.left.val == key){ pre.left = deleteOneNode(cur); } if (pre.right !=null && pre.right.val == key){ pre.right = deleteOneNode(cur); } return root; }
public TreeNode deleteOneNode(TreeNode node){ if (node == null){ return null; } if (node.right == null){ return node.left; } TreeNode cur = node.right; while (cur.left !=null){ cur = cur.left; } cur.left = node.left; return node.right; } }
|