669. 修剪二叉搜索树
题目链接
题目描述:
给定一个二叉搜索树,同时给定最小边界L 和最大边界 R。通过修剪二叉搜索树,使得所有节点的值在[L, R]中 (R>=L) 。你可能需要改变树的根节点,所以结果应当返回修剪好的二叉搜索树的新的根节点。
难点:
思路:
主要操作和上一题BST中删除根节点的操作类似
但是要注意其他细节:
采用前序遍历,
-
需要更新根节点:当根节点(包括更新后的)落在
[low, high]
区间外,要进行连续删除
1.1 如果左右子树均为空:直接返回null
1.2 如果左子树不为空,右子树为空:更新当前根节点为左孩子
1.3 如果左子树为空,右子树不为空:更新当前根节点为右孩子
1.4 如果左右子树均不为空:那就调整BST -
不需要更新根节点:递归遍历左、右子树
class Solution {public TreeNode trimBST(TreeNode root, int low, int high) {if (root == null) return null;while (root.val < low || root.val > high) { //删除根结点(包括更新后的根节点)if (root.left == null && root.right == null) return null;if (root.left == null && root.right != null) { //更新根节点root = root.right;continue;}if (root.right == null && root.left != null) { //更新根节点root = root.left;continue;}TreeNode tmp = root.right;while (tmp.left != null) {tmp = tmp.left;}tmp.left = root.left;root = root.right;}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;}if (root.val < low) {return trimBST(root.right, low, high);}if (root.val > high) {return trimBST(root.left, low, high);}// root在[low,high]范围内root.left = trimBST(root.left, low, high);root.right = trimBST(root.right, low, high);return root;}
}
时长:
20min
收获:
多情况分析
108. 将有序数组转换为二叉搜索树
题目链接
题目描述:
将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树。
本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1。
示例:
难点:
思路:
因为给定数组是有序的,采用二分法进行构造就可以保证BST是平衡的
//左闭右闭[left, right]
class Solution {public TreeNode sortedArrayToBST(int[] nums) {return buildBST(nums, 0, nums.length-1);}public TreeNode buildBST(int[] nums, int left, int right) {if (left > right) return null;int mid = (left+right)/2;TreeNode root = new TreeNode(nums[mid]);root.left = buildBST(nums, left, mid-1);root.right = buildBST(nums, mid+1, right);return root;}
}//左闭右开[left, right)
class Solution {public TreeNode sortedArrayToBST(int[] nums) {return buildBST(nums, 0, nums.length);}public TreeNode buildBST(int[] nums, int left, int right) {if (left >= right) return null;int mid = (left+right)/2;TreeNode root = new TreeNode(nums[mid]);root.left = buildBST(nums, left, mid);root.right = buildBST(nums, mid+1, right);return root;}
}
时长:
6min
收获:
二分法复习
538. 把二叉搜索树转换为累加树
题目链接
题目描述:
给出二叉 搜索 树的根节点,该树的节点值各不相同,请你将其转换为累加树(Greater Sum Tree),使每个节点 node 的新值等于原树中大于或等于 node.val 的值之和。
提醒一下,二叉搜索树满足下列约束条件:
节点的左子树仅包含键 小于 节点键的节点。 节点的右子树仅包含键 大于 节点键的节点。 左右子树也必须是二叉搜索树。
示例 1:
- 输入:[4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
- 输出:[30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
示例 2:
- 输入:root = [0,null,1]
- 输出:[1,null,1]
示例 3:
- 输入:root = [1,0,2]
- 输出:[3,3,2]
示例 4:
- 输入:root = [3,2,4,1]
- 输出:[7,9,4,10]
提示:
树中的节点数介于 0 和 104 之间。
每个节点的值介于 -104 和 104 之间。
树中的所有值 互不相同 。
给定的树为二叉搜索树。
难点:
思路:
根据题目要求以及观察示例
应该采用“右中左”的遍历顺序来构造
class Solution {int curSum;public TreeNode convertBST(TreeNode root) {if (root == null) return null;root.right = convertBST(root.right);curSum += root.val;root.val = curSum;root.left = convertBST(root.left);return root;}
}
时长:
8min
收获:
。。。