235. 二叉搜索树的最近公共祖先
给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
例如,给定如下二叉搜索树: root = [6,2,8,0,4,7,9,null,null,3,5]
示例 1:
输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8 输出: 6 解释: 节点 2 和节点 8 的最近公共祖先是 6。
思路
在236.找二叉树的最近公共节点时,思路是自底向上-回溯-后序遍历,本题可以采用同样的方法;
但这种方法没有利用到搜索树的特性。
利用搜索树的性质,本题思路可以是:自顶向下-深度优先 而递归遍历顺序,本题就不涉及到 前中后序了(这里没有中节点的处理逻辑,遍历顺序无所谓了)。
自底向上-回溯-后序遍历
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == p || root == q || root == null){
return root;
}
TreeNode left = lowestCommonAncestor(root.left, p, q);
TreeNode right = lowestCommonAncestor(root.right, p, q);
if(right == null && left == null) return null;
else if(right != null && left == null) return right;
else if(right == null && left != null) return left;
else return root;
}
}
自顶向下-dfs
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null){
return root;
}
//带返回值的递归函数 遍历一条边的写法
if(root.val < p.val && root.val < q.val){
TreeNode cur = lowestCommonAncestor(root.right, p, q);
if(cur != null) return cur;
}
if(root.val > p.val && root.val > q.val){
TreeNode cur = lowestCommonAncestor(root.left, p, q);
if(cur != null) return cur;
}
//此时root的值在两者中间
return root;
}
}
总结
235.对于二叉搜索树的最近祖先问题,其实要比普通二叉树公共祖先问题 (opens new window)简单的多。
不用使用回溯,二叉搜索树自带方向性,可以方便的从上向下查找目标区间,遇到目标区间内的节点,直接返回。
最后给出了对应的迭代法,二叉搜索树的迭代法甚至比递归更容易理解,也是因为其有序性(自带方向性),按照目标区间找就行了。
701.二叉搜索树中的插入操作
给定二叉搜索树(BST)的根节点 root
和要插入树中的值 value
,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 输入数据 保证 ,新值和原始二叉搜索树中的任意节点值都不同。
注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。 你可以返回 任意有效的结果 。
示例 1:
输入:root = [4,2,7,1,3], val = 5 输出:[4,2,7,1,3,5] 解释:另一个满足题目要求可以通过的树是:
思路
自顶向下地找插入位置,搜索整棵树
递归三步曲
返回值:插入节点后的根节点
终止条件:寻找到了待插入位置
单次递归逻辑:找存在插入位置的根节点,根据val决定。
递归
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);
}
if(root.val > val){
root.left = insertIntoBST(root.left, val);
}
return root;
}
}
迭代
注意pre指针的使用
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;
}
}
递归(无返回值)
是利用pre指针实现,C++的代码:
class Solution {
private:
TreeNode* parent;
void traversal(TreeNode* cur, int val) {
if (cur == NULL) {
TreeNode* node = new TreeNode(val);
if (val > parent->val) parent->right = node;
else parent->left = node;
return;
}
parent = cur;
if (cur->val > val) traversal(cur->left, val);
if (cur->val < val) traversal(cur->right, val);
return;
}
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
parent = new TreeNode(0);
if (root == NULL) {
root = new TreeNode(val);
}
traversal(root, val);
return root;
}
};
总结
701.首先在二叉搜索树中的插入操作,不用恐惧其重构搜索树,其实根本不用重构。
然后在递归中,我们重点讲了如何通过递归函数的返回值完成新加入节点和其父节点的赋值操作,并强调了搜索树的有序性。
最后依然给出了迭代的方法,迭代的方法就需要记录当前遍历节点的父节点了,这个和没有返回值的递归函数实现的代码逻辑是一样的。
450.删除二叉搜索树中的节点
给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。
一般来说,删除节点可分为两个步骤:
- 首先找到需要删除的节点;
- 如果找到了,删除它。
示例 1:
输入:root = [5,3,6,2,4,null,7], key = 3 输出:[5,4,6,2,null,null,7] 解释:给定需要删除的节点值是 3,所以我们首先找到 3 这个节点,然后删除它。 一个正确的答案是 [5,4,6,2,null,null,7], 如上图所示。 另一个正确答案是 [5,2,6,null,4,null,7]。
思路
跟插入类似的思路去找这个节点,若找到了则删除,注意删除需保留二叉搜索树的性质(对左子树/右子树进行处理)
递归
class Solution {
//返回删除节点后的根节点
public TreeNode deleteNode(TreeNode root, int key) {
//没找到
if(root == null){
return root;
}
//找到了,且为叶子节点
if(root.val == key && root.left == null && root.right == null){
return null;
}
//找到了,不为叶子
if(root.val == key){
//优先处理均不为空的情况
if(root.left != null && root.right != null){
//注意 二叉搜索树的删除,是将左子树挂到右子树最左面节点的左边
// 或者 将右子树挂到左子树最右面节点的右面
TreeNode cur = root.left;
while(cur.right != null){
cur = cur.right;
}
//此时cur为左子树的最右面节点
//将右子树挂到cur的右面
cur.right = root.right;
return root.left;
}
if(root.left != null){
return root.left;
}
if(root.right != null){
return root.right;
}
}
if(root.val > key){
root.left = deleteNode(root.left, key);
}
if(root.val < key){
root.right = deleteNode(root.right, key);
}
return root;
}
}
迭代
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;
}
}
总结
读完本篇,大家会发现二叉搜索树删除节点比增加节点复杂的多。
因为二叉搜索树添加节点只需要在叶子上添加就可以的,不涉及到结构的调整,而删除节点操作涉及到结构的调整。
这里我们依然使用递归函数的返回值来完成把节点从二叉树中移除的操作。
这里最关键的逻辑就是第五种情况(删除一个左右孩子都不为空的节点),这种情况一定要想清楚。
而且就算想清楚了,对应的代码也未必可以写出来,所以这道题目既考察思维逻辑,也考察代码能力。
最后我也给出了相应的迭代法,就是模拟递归法中的逻辑来删除节点,但需要一个pre记录cur的父节点,方便做删除操作。
迭代法其实不太容易写出来,所以如果是初学者的话,彻底掌握第一种递归写法就够了。