669. 修剪二叉搜索树
- 刷题https://leetcode.cn/problems/trim-a-binary-search-tree/description/
- 文章讲解https://programmercarl.com/0669.%E4%BF%AE%E5%89%AA%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91.html
- 视频讲解https://www.bilibili.com/video/BV17P41177ud/?share_source=copy_web&vd_source=af4853e80f89e28094a5fe1e220d9062
-
题解(递归法):
/**
* 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 trimBST(TreeNode root, int low, int high) {
//若传入二叉树为空,则直接返回null
if(root == null){
return null;
}
//若当前结点小于规定区间,则当前结点的右分支还有可能在规定区间内
//所以需要对当前结点右分支进行递归判断
if(root.val < low){
TreeNode right = trimBST(root.right, low, high);
//将递归下一层得到的新的右分支根结点返回给当前层的父结点
//当前root结点的删除
return right;
}
//若当前结点大于规定区间,则当前结点的左分支还有可能在规定区间内
//所以需要对当前结点左分支进行递归判断
if(root.val > high){
TreeNode left = trimBST(root.left, low,high);
return left;
}
//若root在规定范围内
//跟结点左分支递归
root.left = trimBST(root.left, low, high);
//根结点右分支递归
root.right = trimBST(root.right, low, high);
//返回新的二叉搜索树的根结点
//前面也已经处理过只有根结点的情况,所以这里不用特殊处理根结点
return root;
}
}
108.将有序数组转换为二叉搜索树
- 刷题https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree/description/
- 文章讲解https://programmercarl.com/0108.%E5%B0%86%E6%9C%89%E5%BA%8F%E6%95%B0%E7%BB%84%E8%BD%AC%E6%8D%A2%E4%B8%BA%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91.html
- 视频讲解https://www.bilibili.com/video/BV1uR4y1X7qL/?share_source=copy_web&vd_source=af4853e80f89e28094a5fe1e220d9062
-
题解(递归法):
/**
* 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 sortedArrayToBST(int[] nums) {
return sortedArrayToBST(nums, 0, nums.length - 1);
}
public TreeNode sortedArrayToBST(int[] nums, int left, int right){
//因为左闭右闭,所以left = right 为合法区间
//则left > right为非法区间
if(left > right){
return null;
}
//若只有一个结点,则直接充当根结点,返回
if(right - left == 0){
return new TreeNode(nums[left]);
}
//若有两个及以上结点,进行递归
//注意左闭右闭的递归对应
int mid =( left + right ) / 2;
TreeNode root = new TreeNode(nums[mid]);
//左递归
root.left = sortedArrayToBST(nums, left, mid - 1);
//右递归
root.right = sortedArrayToBST(nums, mid + 1, right);
//返回根结点
return root;
}
}
538.把二叉搜索树转换为累加树
- 刷题https://leetcode.cn/problems/convert-bst-to-greater-tree/description/
- 文章讲解https://programmercarl.com/0538.%E6%8A%8A%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E8%BD%AC%E6%8D%A2%E4%B8%BA%E7%B4%AF%E5%8A%A0%E6%A0%91.html
- 视频讲解https://www.bilibili.com/video/BV1d44y1f7wP/?share_source=copy_web&vd_source=af4853e80f89e28094a5fe1e220d9062
-
题解(递归法):
/**
* 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 {
//累加
//记录累加和
int sum;
public TreeNode convertBST(TreeNode root) {
sum = 0;
convertBST1(root);
return root;
}
//由题意得,按右中左顺序遍历,进行累加即可
public void convertBST1(TreeNode root){
//若传入空树,则直接返回
if(root == null){
return;
}
//右处理
convertBST1(root.right);
//中处理
//记录当前累加和
sum += root.val;
//将当前累加和赋值给当前结点
root.val = sum;
//左处理
convertBST1(root.left);
}
}