链接: link
先用中序遍历把BST结构保存在res中;
然后再遍历一遍res,如果有key则删除,没有则return root;
最后重构BST
/**
* 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 deleteNode(TreeNode root, int key) {
List<Integer> res = new ArrayList<>();
inorder(root, res);
if (res.contains(key)) {
for (int i = 0; i < res.size(); i++) {
if (res.get(i) == key) {
res.remove(Integer.valueOf(key));
}
}
} else {
return root;
}
return buildBST(res, 0, res.size() - 1);
}
void inorder(TreeNode root, List<Integer> res) {
if (root == null)
return;
inorder(root.left, res);
res.add(root.val);
inorder(root.right, res);
}
private TreeNode buildBST(List<Integer> res, int left, int right) {
if (left > right) {
return null;
}
int mid = left + (right - left) / 2; // 选择中间元素作为根节点
TreeNode root = new TreeNode(res.get(mid));
root.left = buildBST(res, left, mid - 1); // 左子树
root.right = buildBST(res, mid + 1, right); // 右子树
return root;
}
}
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
if (root == null) return root; // 没找到
if (root.val == key) { // 包含了删除节点是叶子节点的情况
if (root.left == null) {//左空右不空
return root.right;
} else if (root.right == null) { // 左不空右空
return root.left;
} else {// 左不空 右不空
TreeNode cur = root.right;
while (cur.left != null) {
cur = cur.left;
}
cur.left = root.left;
root = root.right;
return root;
}
}
if (root.val > key) root.left = deleteNode(root.left, key);
if (root.val < key) root.right = deleteNode(root.right, key);
return root;
}
}
669.修剪二叉搜索树
链接: link
class Solution {
public TreeNode trimBST(TreeNode root, int low, int high) {
// 递归修剪树
if (root == null) {
return null;
}
// 如果当前节点值小于 low,剪掉左子树
if (root.val < low) {
return trimBST(root.right, low, high); // 跳过左子树,递归修剪右子树
}
// 如果当前节点值大于 high,剪掉右子树
if (root.val > high) {
return trimBST(root.left, low, high); // 跳过右子树,递归修剪左子树
}
// 当前节点值在 [low, high] 范围内,修剪其左右子树
root.left = trimBST(root.left, low, high); // 递归修剪左子树
root.right = trimBST(root.right, low, high); // 递归修剪右子树
return root; // 返回修剪后的根节点
}
}
如果题目不要求保持原来结构的话 这种方法更容易想到
/**
* 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) {
List<Integer> res = new ArrayList<>();
inorder(root, res);
// 倒序删除不符合条件的元素
for (int i = res.size() - 1; i >= 0; i--) {
if (res.get(i) < low || res.get(i) > high) {
res.remove(i);
}
}
// 使用修剪后的值重新构建二叉树
return buildBST(res, 0, res.size() - 1);
}
// 中序遍历
void inorder(TreeNode root, List<Integer> res) {
if (root == null)
return;
inorder(root.left, res);
res.add(root.val);
inorder(root.right, res);
}
// 根据数组构建二叉搜索树
private TreeNode buildBST(List<Integer> res, int left, int right) {
if (left > right) {
return null;
}
int mid = left + (right - left) / 2; // 选择中间元素作为根节点
TreeNode root = new TreeNode(res.get(mid));
root.left = buildBST(res, left, mid - 1); // 左子树
root.right = buildBST(res, mid + 1, right); // 右子树
return root;
}
}