本次题目
- 701 二叉搜索树中的插入操作
- 450 删除二叉搜索树中的节点
- 669 修剪二叉搜索树
- 108 将有序数组转换为二叉搜索树
701 二叉搜索树中的插入操作
- 递归:遍历二叉搜索树,找到空节点位置插入。
- 输入根节点和待插入元素,返回元素插入的新节点同时完成赋值;
- 当前节点为空时,插入节点,返回新节点;
- 待插入元素大于当前节点值时,向右递归,否则向左递归,递归的同时赋值。
class Solution {
public TreeNode insertIntoBST(TreeNode root, int val) {
//直接递归
//当前节点为空时,插入节点,返回新节点
if(root == null) return new TreeNode(val);
//待插入元素大于当前节点值时,向右递归,否则向左递归,递归的同时赋值
if(val > root.val) root.right = insertIntoBST(root.right, val);
if(val < root.val) root.left = insertIntoBST(root.left, val);
//最后返回结果
return root;
}
}
class Solution {
public TreeNode insertIntoBST(TreeNode root, int val) {
//二叉搜索树为空时直接返回
if(root == null) return new TreeNode(val);
TreeNode node = root;
//需要记录当前节点的父节点,完成插入
TreeNode pre = null;
//迭代,当节点为空时结束
while(node != null){
//更新上一个节点
pre = node;
if(val > node.val){
node = node.right;
}else{
node = node.left;
}
}
//找到插入位置,开始插入
TreeNode ins = new TreeNode(val);
if(val > pre.val){
pre.right = ins;
}else{
pre.left = ins;
}
return root;
}
}
450 删除二叉搜索树中的节点
- 递归:输入根节点和要删除的值,返回要删除的节点并赋值给要删除节点的父节点;遍历到最后,当节点为空时直接返回;当前节点值大于要删除的值时向左递归,小于时向右递归,同时赋值(不用删除操作),
- 最后没找到删除的节点,返回空。
找到要删除的节点后, - 如果要删除的节点左右孩子都为空,直接删除节点,返回空;
- 如果要删除节点的左孩子为空,右孩子不空,删除节点后右孩子补位到删除节点的位置,返回右孩子;
- 如果要删除节点的右孩子为空,左孩子不空,删除节点后左孩子补位到删除节点的位置,返回左孩子;
- 如果要删除节点的左右孩子都不空,删除节点后将左孩子移动到右孩子最左边节点的左孩子上,右孩子补位到删除节点的位置,返回右孩子。
- 补充:针对普通二叉树的递归。
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
//直接递归
//1.遍历到最后,没找到删除的节点,当节点为空时返回空。
if(root == null) return null;
//找到要删除的节点后
if(root.val == key){
//2.如果要删除的节点左右孩子都为空,直接删除节点,返回空
if(root.left == null && root.right == null) return null;
//3.如果要删除节点的左孩子为空,右孩子不空,
//删除节点后右孩子补位到删除节点的位置,返回右孩子
if(root.left == null && root.right != null) return root.right;
//4.如果要删除节点的右孩子为空,左孩子不空,
//删除节点后左孩子补位到删除节点的位置,返回左孩子
if(root.left != null && root.right == null) return root.left;
//5.如果要删除节点的左右孩子都不空,
//删除节点后将左孩子移动到右孩子最左边节点的左孩子上,
//右孩子补位到删除节点的位置,返回右孩子
TreeNode node = root.right;
while(node.left != null){
node = node.left;
}
//此时node为右孩子最左边节点位置,其左孩子为空
node.left = root.left;
return root.right;
}
//当前节点值大于要删除的值时向右递归,小于时向左递归,同时赋值(不用删除操作)
if(root.val > key) root.left = deleteNode(root.left, key);
if(root.val < key) root.right = deleteNode(root.right, key);
//最后返回root
return root;
}
}
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
//当节点为空时返回空。
if(root == null) return null;
//当只有一个节点,就是要删除节点时,直接返回空
//if(root.val == key && root.left == null && root.right == null) return null;
//定义当前节点及其父节点
TreeNode node = root;
TreeNode pre = null;
//开始迭代,当前节点为空时结束
while(node != null){
//中序遍历
if(node.val == key) break;
//修改上一个节点
pre = node;
if(node.val > key){
node = node.left;
}else if(node.val < key){
node = node.right;
}
}
//若上一个节点为空, 则直接删除该节点节点
if(pre == null) return deleteN(node);
//1.迭代结束,若没找到,则最后node为空,返回原树
if(node == null) return root;
//找到了,node即为要删除的节点,pre为node的父节点
//但是不知道node是pre的左孩子还是右孩子
//需要判断pre的左孩子右孩子
if(pre.left != null && pre.left.val == key){
//定义删除函数
pre.left = deleteN(node);
}else if(pre.right != null && pre.right.val == key){
pre.right = deleteN(node);
}
//最后返回结果
return root;
}
//删除函数,输入要删除的节点,返回删除后补位的节点
private TreeNode deleteN(TreeNode root){
//2.如果要删除的节点左右孩子都为空,直接删除节点,返回空
if(root.left == null && root.right == null) return null;
//3.如果要删除节点的左孩子为空,右孩子不空
//删除节点后右孩子补位到删除节点的位置,返回右孩子
if(root.left == null && root.right != null) return root.right;
//如果要删除节点的右孩子为空,左孩子不空
//删除节点后左孩子补位到删除节点的位置,返回左孩子
if(root.left != null && root.right == null) return root.left;
//如果要删除节点的左右孩子都不空,
//删除节点后将左孩子移动到右孩子最左边节点的左孩子上,
//右孩子补位到删除节点的位置,返回右孩子
TreeNode node = root.right;
while(node.left != null){
node = node.left;
}
//此时node为右孩子最左边节点位置,其左孩子为空
node.left = root.left;
return root.right;
}
}
669 修剪二叉搜索树
- 递归:
- 输入根节点和区间上下限,返回符合条件的节点;
- 当前节点为空时直接返回;
- 如果当前节点值小于下限,则递归右子树,返回符合条件的节点,如果当前节点值大于上限,则递归左子树,返回符合条件的节点,然后分别赋值给当前节点的左右子树。
class Solution {
public TreeNode trimBST(TreeNode root, int low, int high) {
//直接递归
//当前节点为空时直接返回
if(root == null) return null;
//如果当前节点值小于下限,则递归右子树,返回符合条件的节点
if(root.val < low) return trimBST(root.right, low, high);
//如果当前节点值大于上限,则递归左子树,返回符合条件的节点
if(root.val > high) return trimBST(root.left, low, high);
//然后分别赋值给当前节点的左右子树
root.left = trimBST(root.left, low, high);
root.right = trimBST(root.right, low, high);
//最后返回结果
return root;
}
}
class Solution {
public TreeNode trimBST(TreeNode root, int low, int high) {
//当前节点为空时直接返回
if(root == null) return null;
//当根节点不在区间内时,修改根节点
while(root != null){
if(root.val < low){
root = root.right;
}else if(root.val > high){
root = root.left;
}else{
break;
}
}
//根节点在区间内,分别判断左右子树
TreeNode node = root;
while(node != null){
//修改左孩子
while(node.left != null && node.left.val < low){
node.left = node.left.right;
}
//修改当前节点
node = node.left;
}
node = root;
while(node != null){
//修改右孩子
while(node.right != null && node.right.val > high){
node.right = node.right.left;
}
//修改当前节点
node = node.right;
}
//返回结果
return root;
}
}
108 将有序数组转换为二叉搜索树
- 递归:取数组中间点作为分割点。
- 输入数组及其左右区间(左闭右闭),返回下一次的分割点(根节点);
- 当左区间大于右区间时结束;
- 根据左右区间选取数组的分割点,以分割点构造节点,然后递归左右孩子,最后返回根节点。
- 注意:取左右区间中间时小心越界。区间选取时,左右区间中间有一个为分割点,不能进入下一次递归。
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
//开始递归
TreeNode root = sortedArrayToBST1(nums, 0, nums.length - 1);
//返回结果
return root;
}
//递归函数
//输入数组及其左右区间(左闭右闭),返回下一次的分割点(根节点)
private TreeNode sortedArrayToBST1(int[] nums, int left, int right){
//当左区间大于右区间时结束
if(left > right) return null;
//取数组中间点作为分割点
int mid = left + (right - left) / 2;
//根据左右区间选取数组的分割点,以分割点构造节点
TreeNode root = new TreeNode(nums[mid]);
//然后递归左右孩子
root.left = sortedArrayToBST1(nums, left, mid - 1);
root.right = sortedArrayToBST1(nums, mid + 1, right);
//最后返回结果
return root;
}
}
- 迭代(使用三个队列,一个放遍历的节点,一个放左区间下标,一个放右区间下标):
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
//数组为空时直接返回
if(nums.length == 0) return null;
//使用三个队列,队列1放遍历的节点,队列2放左区间下标,队列3放右区间下标
Queue<TreeNode> queue1 = new LinkedList<>();
Queue<Integer> queue2 = new LinkedList<>();
Queue<Integer> queue3 = new LinkedList<>();
//初始化
TreeNode root = new TreeNode(0);
queue1.offer(root);
queue2.offer(0);
queue3.offer(nums.length - 1);
//开始迭代,左闭右闭,队列1为空时结束
while(!queue1.isEmpty()){
//弹出需要处理的元素
TreeNode cur = queue1.poll();
int left = queue2.poll();
int right = queue3.poll();
//取数组中间点作为分割点
int mid = left + (right - left) / 2;
//直接赋值
cur.val = nums[mid];
//处理左区间
if(left <= mid - 1){
cur.left = new TreeNode(0);
queue1.offer(cur.left);
queue2.offer(left);
queue3.offer(mid - 1);
}
//处理右区间
if(right >= mid + 1){
cur.right = new TreeNode(0);
queue1.offer(cur.right);
queue2.offer(mid + 1);
queue3.offer(right);
}
}
//返回结果
return root;
}
}