算法训练营(day23)

669. 修剪二叉搜索树

题目链接:https://leetcode.cn/problems/trim-a-binary-search-tree/description/

给你二叉搜索树的根节点 root ,同时给定最小边界low 和最大边界 high。通过修剪二叉搜索树,使得所有节点的值在[low, high]中。修剪树 不应该 改变保留在树中的元素的相对结构 (即,如果没有被移除,原有的父代子代关系都应当保留)。 可以证明,存在 唯一的答案

所以结果应当返回修剪好的二叉搜索树的新的根节点。注意,根节点可能会根据给定的边界发生改变。

解题思路

解题过程:

思路一:递归

  • 判断节点大小是否处于边界内

  • 如果满足则保留

  • 如果不满足,需要考虑节点的子树节点是否会满足边界条件

    • 左子树 > low
    • 右子树 < high
  • 返回修剪好的 二叉搜索树

思路二:迭代

  • 和递归的思路一致

详细代码

解法一:递归

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* 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) {
if(root == null){
return root;
}
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;
}
}

解法二:迭代

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* 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) {
while (root != null && (root.val < low || root.val > high)) root = root.val < low ? root.right : root.left;
TreeNode ans = root;
while (root != null) {
while (root.left != null && root.left.val < low) root.left = root.left.right;
root = root.left;
}
root = ans;
while (root != null) {
while (root.right != null && root.right.val > high) root.right = root.right.left;
root = root.right;
}
return ans;
}
}

108. 将有序数组转换为二叉搜索树

题目链接:https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree/description/

给你一个整数数组 nums ,其中元素已经按 升序 排列,请你将其转换为一棵 高度平衡 二叉搜索树。

高度平衡 二叉树是一棵满足「每个节点的左右两个子树的高度差的绝对值不超过 1 」的二叉树。

解题思路

解题过程:高度平衡, 说明二叉搜索树的根节点应该为 数组的中值

解法一:递归

  • 求出数组中值
  • 左右子树递归求二叉树

解法二:迭代(双指针)

  • 和递归的思路一致
  • 用三个队列存储 根节点、左节点和右节点,在依次遍历下去

详细代码

解法一:递归

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* 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);
}

public TreeNode sortedArrayToBST(int[] nums, int left, int right){
if(left >= right){
return null;
}
if(right - left == 1){
return new TreeNode(nums[left]);
}

int mid = left + (right - left) / 2;
TreeNode root = new TreeNode(nums[mid]);
root.left = sortedArrayToBST(nums, left, mid);
root.right = sortedArrayToBST(nums, mid + 1, right);
return root;
}
}

解法二:迭代

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* 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) {
if(nums == null || nums.length == 0){
return null;
}

TreeNode root = new TreeNode(-1);
Queue<TreeNode> nodeQueue = new LinkedList<>();
Queue<Integer> leftQueue = new LinkedList<>();
Queue<Integer> rightQueue = new LinkedList<>();

nodeQueue.offer(root);
leftQueue.offer(0);
rightQueue.offer(nums.length - 1);

while(!nodeQueue.isEmpty()){
TreeNode cur = nodeQueue.poll();
int left = leftQueue.poll();
int right = rightQueue.poll();
int mid = left + ((right - left) >> 1);

cur.val = nums[mid];

if(left <= mid - 1){
cur.left = new TreeNode(-1);
nodeQueue.offer(cur.left);
leftQueue.offer(left);
rightQueue.offer(mid - 1);
}

if(right >= mid + 1){
cur.right = new TreeNode(-1);
nodeQueue.offer(cur.right);
leftQueue.offer(mid + 1);
rightQueue.offer(right);
}
}
return root;
}
}

538. 把二叉搜索树转换为累加树

题目链接:https://leetcode.cn/problems/convert-bst-to-greater-tree/description/

给出二叉 搜索 树的根节点,该树的节点值各不相同,请你将其转换为累加树(Greater Sum Tree),使每个节点 node 的新值等于原树中大于或等于 node.val 的值之和。

提醒一下,二叉搜索树满足下列约束条件:

  • 节点的左子树仅包含键 小于 节点键的节点。
  • 节点的右子树仅包含键 大于 节点键的节点。
  • 左右子树也必须是二叉搜索树。

解题思路

解题过程:从 右子树的最右节点 一路遍历累加

解法一:递归

  • 右子树的最右节点 一路遍历累加

解法二:迭代

  • 将二叉搜索树的右节点全部压入,栈顶就是 右子树的最右节点
  • 累加节点值,并将累加值覆盖原节点值
  • 返回累加树

详细代码

解法一:递归

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* 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 = 0;
public TreeNode convertBST(TreeNode root) {
convert(root);
return root;
}

public void convert(TreeNode node){
if(node == null){
return;
}
convert(node.right);
sum += node.val;
node.val = sum;
convert(node.left);
}
}

解法二:迭代

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* 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 convertBST(TreeNode root) {
if(root == null){
return root;
}
int sum = 0;
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while(cur != null || !stack.isEmpty()){
while(cur != null){
stack.push(cur);
cur = cur.right;
}
TreeNode tmp = stack.pop();
sum += tmp.val;
tmp.val = sum;
cur = tmp.left;
}
return root;
}
}