235. 二叉搜索树的最近公共祖先相对于 二叉树的最近公共祖先 本题就简单一些了,因为 可以利用二叉搜索树的特性。
题目链接/文章讲解:代码随想录
因为是有序树,所以 如果 中间节点是 q 和 p 的公共祖先,那么 中节点的数组 一定是在 [p, q]区间的。即 中节点 > p && 中节点 < q 或者 中节点 > q && 中节点 < p。
那么只要从上到下去遍历,遇到 cur节点是数值在[p, q]区间中则一定可以说明该节点cur就是q 和 p的公共祖先。一定是最近公共祖先吗?一定是,因为p和q分别在cur的左右子树上
当我们从上向下去递归遍历,第一次遇到 cur节点是数值在[p, q]区间中,那么cur就是 p和q的最近公共祖先。二叉搜索树确定了搜索方向,所以代码很简单。
递归法
注意递归后要承接递归传回的参数,进行处理
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null) return root;
if(p == null || q == null) return root;
int leftVal = p.val;
int rightVal = q.val;
if(p.val > q.val){
leftVal = q.val;
rightVal = p.val;
}
// 中
// 左
if(root.val > rightVal){
TreeNode left = lowestCommonAncestor(root.left, p, q);
if(left != null) return left;
}else if(root.val < leftVal){// 右
TreeNode right = lowestCommonAncestor(root.right, p, q);
if(right != null) return right;
}else{
return root;
}
return null;
}
}
迭代法
利用其有序性,迭代的方式还是比较简单的,解题思路在递归中已经分析了。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null) return root;
if(p == null || q == null) return root;
int leftVal = p.val;
int rightVal = q.val;
if(p.val > q.val){
leftVal = q.val;
rightVal = p.val;
}
while(root != null){
if(root.val > rightVal){
root = root.left;
}else if(root.val < leftVal){
root = root.right;
}else{
return root;
}
}
return null;
}
}
701.二叉搜索树中的插入操作本题比想象中的简单,大家可以先自己想一想应该怎么做,然后看视频讲解,就发现 本题为什么比较简单了。
题目链接/文章讲解:代码随想录
可以不考虑题目中提示所说的改变树的结构的插入方式。添加节点不用改动二叉搜索树的结构,在叶子节点位置就一定能找到要添加节点的位置。
只要按照二叉搜索树的规则去遍历,遇到空节点就插入节点就可以了。那么这道题其实就简单了。在叶子节点上找到插入新节点的位置。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode insertIntoBST(TreeNode root, int val) {
if(root == null){// 如果当前节点为空,也就意味着val找到了合适的位置,此时创建节点直接返回。
TreeNode newNode = new TreeNode(val);
return newNode;
}
if(root.val > val){
root.left = insertIntoBST(root.left, val);// 递归创建左子树
}else{
root.right = insertIntoBST(root.right, val);// 递归创建右子树
}
return root;
}
}
450.删除二叉搜索树中的节点
二叉搜索树删除节点比增加节点复杂的多。因为二叉搜索树添加节点只需要在叶子上添加就可以的,不涉及到结构的调整,而删除节点操作涉及到结构的调整。
有以下五种情况:
- 第一种情况:没找到删除的节点,遍历到空节点直接返回了
- 找到删除的节点
- 第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点
- 第三种情况:删除节点的左孩子为空,右孩子不为空,删除节点,右孩子补位,返回右孩子为根节点
- 第四种情况:删除节点的右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点
- 第五种情况:左右孩子节点都不为空,则将删除节点的左子树头结点(左孩子)放到删除节点的右子树的最左面节点的左孩子上,返回删除节点右孩子为新的根节点。
- 这里最关键的逻辑就是第五种情况(删除一个左右孩子都不为空的节点)
调用递归函数相当于把新的节点返回给上一层,上一层就要用 root->left 或者 root->right接住
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
//找到目标节点就要进行删除操作,删除节点的操作就在终止条件中,因此很复杂
if(root == null) return null;//第一种情况:没找到删除的节点,遍历到空节点直接返回了
if(root.val == key){
// 第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点
if(root.left == null && root.right == null){
return null;
}
// 第三种情况:其右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点
else if(root.left != null && root.right == null){
return root.left;
}
// 第四种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点
else if(root.left == null && root.right != null){
return root.right;
}
// 第五种情况:左右孩子节点都不为空,则将删除节点的左子树放到删除节点的右子树的最左面节点的左孩子的位置
// 并返回删除节点右孩子为新的根节点。
else{
TreeNode cur = root.right;
// 找右子树最左面的节点
while(cur.left != null){
cur = cur.left;
}
cur.left = root.left;// 把要删除的节点(root)左子树放在cur的左孩子的位置
return root.right;// 返回旧root的右孩子作为新root
}
}
if(key < root.val) root.left = deleteNode(root.left, key);
if(key > root.val) root.right = deleteNode(root.right, key);
//如果root==key,直接就在上面处理了
return root;
}
}
普通二叉树的删除方式
一种通用的删除,普通二叉树的删除方式(没有使用搜索树的特性,遍历整棵树),用交换值的操作来删除目标节点。
代码中目标节点(要删除的节点)被操作了两次:
- 第一次是和目标节点的右子树最左面节点交换。
- 第二次直接被NULL覆盖了。
如果根节点 root
的值等于 key
,说明我们找到了要删除的目标节点:
- 如果目标节点的右子树为空(
root->right == nullptr
),则直接将根节点的左子树作为新的根节点,完成删除操作。 - 否则,我们需要找到目标节点的右子树中的最左面的节点
cur
,并将它的值与目标节点的值进行交换。这是为了保持二叉搜索树的性质:右子树中的节点值都大于目标节点,左子树中的节点值都小于目标节点。交换目标值之后,实际上变相地将目标节点的删除操作转换为删除cur
这个节点(由于cur
是目标节点右子树的最左面节点,其左子树为空,所以删除cur
是不会影响树的结构的)。
// 解法2
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;
}
}