二叉树day12
701. 二叉搜索树中的插入操作
//递归遍历搜索树
class Solution {
public TreeNode insertIntoBST(TreeNode root, int val) {
if (root == null) return new TreeNode(val);
if (val > root.val) root.right = insertIntoBST(root.right, val);
else if(val < root.val) root.left = insertIntoBST(root.left, val);
return root;
}
}

//迭代
class Solution {
public TreeNode insertIntoBST(TreeNode root, int val) {
if (root == null) return new TreeNode(val);
TreeNode cur = root, pre = root;
while (cur != null) {
pre = cur;
if (cur.val > val) cur = cur.left;
else cur = cur.right;
}
if (pre.val > val) pre.left = new TreeNode(val);
else pre.right = new TreeNode(val);
return root;
}
}

450. 删除二叉搜索树中的节点
/* 递归
* 如果找到key则重构,所有可能出现的情况如下
* 1.有一个子树为空,直接返回不空的子树
* 2.子树都空返回null
* 3.子树都不为空,删除根节点, 左子树作为右子树最左子结点的左子树
*/
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
if (root == null) return null;
//根节点为key则重构
else if (root.val == key) {
if (root.left == null && root.right == null) return null;
else if(root.left == null && root.right != null) return root.right;
else if(root.left != null && root.right == null) return root.left;
else {
TreeNode cur = root.right;
while (cur.left != null) {
cur = cur.left;
}
cur.left = root.left;
return root.right;
}
}
//没有找到 根据搜索树的特点找key
if (root.val > key) root.left = deleteNode(root.left, key);
else if(root.val < key) root.right = deleteNode(root.right, key);
return root;
}
}

迭代法
//迭代
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
if (root == null) return root;
TreeNode parent = null, cur = root;
while (cur != null) {
if (cur.val == key) break;
parent = cur;
if (cur.val > key) cur = cur.left;
else cur = cur.right;
}
//根val为key
if (parent == null) root = rebuild(cur);
else if (parent.left != null && parent.left.val == key) parent.left = rebuild(cur);
else if (parent.right != null && parent.right.val == key) parent.right = rebuild(cur);
return root;
}
private TreeNode rebuild(TreeNode root) {
//没找到
if (root == null) return null;
else if (root.right == null) return root.left;
//将左子树作为右子树最左子结点的左子树 包含了左子树为空的情况
TreeNode cur = root.right;
while (cur.left != null) {
cur = cur.left;
}
cur.left = root.left;
return root.right;
}
}

669. 修剪二叉搜索树
//递归
class Solution {
public TreeNode trimBST(TreeNode root, int low, int high) {
if (root == null) return null;
if (root.val < low) {
if (root.right == null) return null;
//继续修剪右子树
else return trimBST(root.right, low, high);
} else if (root.val > high) {
if (root.left == null) return null;
//继续修剪左子树
else return trimBST(root.left, low, high);
}
//小于low是要被裁剪的
if (root.val >= low) root.left = trimBST(root.left, low, high);
if (root.val <= 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;
//找到位于[low, high]区间内的结点作为根节点,然后修剪子树
while (root != null && (root.val > high || root.val < low)) {
if (root.val > high) root = root.left;
else root = root.right;
}
//修剪子树
TreeNode cur = root;
//修剪左子树中小于low的部分
while (cur != null) {
while (cur.left != null && cur.left.val < low) {
cur.left = cur.left.right;
}
cur = cur.left;
}
cur = root;
//修剪右子树大于high的部分
while (cur != null) {
while (cur.right != null && cur.right.val > high) {
cur.right = cur.right.left;
}
cur = cur.right;
}
return root;
}
}


浙公网安备 33010602011771号