第二十三天| 第六章 二叉树part09 669. 修剪二叉搜索树 108.将有序数组转换为二叉搜索树 538. 把二叉搜索树转换为累加树
一、669. 修剪二叉搜索树
-
题目链接:https://leetcode.cn/problems/trim-a-binary-search-tree/
-
题目介绍:
-
给你二叉搜索树的根节点
root
,同时给定最小边界low
和最大边界high
。通过修剪二叉搜索树,使得所有节点的值在[low, high]
中。修剪树 不应该 改变保留在树中的元素的相对结构 (即,如果没有被移除,原有的父代子代关系都应当保留)。 可以证明,存在 唯一的答案 。所以结果应当返回修剪好的二叉搜索树的新的根节点。注意,根节点可能会根据给定的边界发生改变。
-
-
思路:
- 如果root(当前节点)的元素小于low的数值,那么应该递归右子树,并返回右子树符合条件的头结点。
- 如果root(当前节点)的元素大于high的,那么应该递归左子树,并返回左子树符合条件的头结点。
- 接下来要将下一层处理完左子树的结果赋给root->left,处理完右子树的结果赋给root->right。
- 可以画一个最一般的情况来思考这个逻辑:
- 代码:
class Solution {
public TreeNode trimBST(TreeNode root, int low, int high) {
if (root == null) return null;
if (root.val < low) {
TreeNode right = trimBST(root.right, low, high);
return right;
}
if (root.val > high) {
TreeNode left = trimBST(root.left, low, high);
return left;
}
root.left = trimBST(root.left, low, high);
root.right = trimBST(root.right, low, high);
return root;
}
}
注意:
我第一次写的时候把代码误写成这样:
这里的问题是:你不能确定root.right和root.left就是满足区间条件的,同样也需要递归去找到符合条件的子树的root头节点
二、108.将有序数组转换为二叉搜索树
-
题目链接:https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree/description/
-
题目介绍:
-
给你一个整数数组 nums ,其中元素已经按 升序 排列,请你将其转换为一棵 高度平衡 二叉搜索树。
高度平衡 二叉树是一棵满足「每个节点的左右两个子树的高度差的绝对值不超过 1 」的二叉树。
-
思路:
- 和以往构造二叉树的思路是一样的
- 只不过本题强调的是要构建的二叉搜索树是一颗平衡二叉树
- 所以我们每次从数组中构建root节点的值就是数组==最中间的数==,这里只要定义好区间即可(我选择左闭右开)
-
代码:
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
int beginIndex = 0;
int endIndex = nums.length;
return traversal(nums, beginIndex, endIndex);
}
public TreeNode traversal(int[] nums, int beginIndex, int endIndex) {
if (beginIndex >= endIndex) return null;
int rootIndex = beginIndex + (endIndex - beginIndex) / 2;
TreeNode root = new TreeNode(nums[rootIndex]);
root.left = traversal(nums, beginIndex, rootIndex);
root.right = traversal(nums, rootIndex + 1, endIndex);
return root;
}
}
三、538. 把二叉搜索树转换为累加树 == 1038. 从二叉搜索树到更大和树
-
题目链接
- https://leetcode.cn/problems/convert-bst-to-greater-tree/
- https://leetcode.cn/problems/binary-search-tree-to-greater-sum-tree/
-
题目介绍:
-
给出二叉 搜索 树的根节点,该树的节点值各不相同,请你将其转换为累加树(Greater Sum Tree),使每个节点
node
的新值等于原树中大于或等于node.val
的值之和。提醒一下,二叉搜索树满足下列约束条件:
- 节点的左子树仅包含键 小于 节点键的节点。
- 节点的右子树仅包含键 大于 节点键的节点。
- 左右子树也必须是二叉搜索树。
-
-
思路:
- 反中序遍历 + 双指针
- 反中序遍历:右中左,因为先要找到原二叉搜索树最大的值开始加,这个最大的最大值就在二叉搜索树的右下角。(可以这么想:中序遍历是递增,而本题要从值最大的节点开始,所以是反中序遍历)。
- 双指针:pre为前一个节点的val,root为当前节点的val(注意这里直接用值,不用节点,可以简单一点)
- 反中序遍历:右中左,因为先要找到原二叉搜索树最大的值开始加,这个最大的最大值就在二叉搜索树的右下角。(可以这么想:中序遍历是递增,而本题要从值最大的节点开始,所以是反中序遍历)。
- 反中序遍历 + 双指针
-
代码:
// pre是int
class Solution {
int pre = 0;
public TreeNode bstToGst(TreeNode root) {
traversal(root);
return root;
}
public void traversal(TreeNode root) {
if (root == null) return;
// 右
traversal(root.right);
//
root.val += pre;
pre = root.val;
traversal(root.left);
}
}
// pre是TreeNode
class Solution {
TreeNode pre = null;
public TreeNode convertBST(TreeNode root) {
traversal(root);
return root;
}
public void traversal (TreeNode root) {
if (root == null) return;
// 右
traversal(root.right);
// 中
if (pre != null) {
root.val += pre.val;
}
pre = root;
// 左
traversal(root.left);
}
}