【LeetCode】day20:235 - 二叉搜索树的最近公共祖先,701 - 二叉搜索树中的插入操作, 450 - 删除二叉搜索树中的节点
LeetCode 代码随想录跟练 Day20
235.二叉搜索树的最近公共祖先
题目描述:
在上一篇文章中做过了二叉树的最近公共祖先,是通过后序遍历的回溯法将结果层层上传。对于二叉搜索树来说可以根据其性质优化流程:当前节点的值大于左子树所有节点值,小于右子树所有节点值。
使用同样的流程进行判断,若当前节点值介于p和q之间,则说明p和q位于当前节点的左右子树中,而小于或大于则说明均位于左子树或右子树中,向该方向移动。为简化逻辑判断流程,调整p和q参数的顺序使当前递归中的p值始终小于q。代码如下:
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (root == nullptr) return nullptr;
if (p->val > q->val) return lowestCommonAncestor(root, q, p);
if ((root->val >= p->val && root->val <= q->val)) {
return root;
} else if (root->val < p->val) {
return lowestCommonAncestor(root->right, p, q);
} else {
return lowestCommonAncestor(root->left, p, q);
}
}
};
701.二叉搜索树中的插入操作
题目描述:
给定二叉搜索树(BST)的根节点 root 和要插入树中的值 value ,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 输入数据 保证 ,新值和原始二叉搜索树中的任意节点值都不同。
注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。 你可以返回 任意有效的结果。
根据二叉搜索树的性质直接插入或递归插入即可,思路都是根据大小向左或向右移动直到移动到nullptr,新建节点并将其根据大小赋给nullptr上一步的节点的左节点或右节点。递归实现代码如下:
class Solution {
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
if (root == nullptr) {
return new TreeNode(val);
}
if (root->val > val) {
root->left = insertIntoBST(root->left, val);
}
if (root->val < val) {
root->right = insertIntoBST(root->right, val);
}
return root;
}
};
根据当前节点迭代实现更为直接:
class Solution {
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
TreeNode* node = new TreeNode(val);
if (root == nullptr) {
return node;
}
TreeNode* cur = root;
TreeNode* pre = root;
while (cur) {
pre = cur;
if (cur->val > val) {
cur = cur->left;
} else {
cur = cur->right;
}
}
if (pre->val > val) {
pre->left = node;
} else {
pre->right = node;
}
return root;
}
};
450.删除二叉搜索树中的节点
题目描述:
给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。
一般来说,删除节点可分为两个步骤:
首先找到需要删除的节点;
如果找到了,删除它。
对于要删除的节点,存在以下三种情况:
- 该节点为叶子节点
- 该节点只有一个子节点
- 该有两个子节点
对于叶子节点,在递归中直接执行删除返回nullptr即可;对于只有一个子节点的节点,返回其另一个不为nullptr的节点即可。重点要有两个子节点的情况,由于是二叉搜索树,右子树中的所有节点值均大于当前节点值,因此也均大于左子树的所有节点值,而右子树的最小元素位于最左叶子节点,所以将当前节点的左子树 作为右子树的最左叶子节点的左子树 是符合二叉搜索树的规则的。代码如下:
class Solution {
public:
TreeNode* deleteNode(TreeNode* root, int key) {
if (root == nullptr) return nullptr;
if (root->val < key) {
root->right = deleteNode(root->right, key);
} else if (root->val > key) {
root->left = deleteNode(root->left, key);
} else {
if (!root->left && !root->right) {
delete root;
return nullptr;
} else if (!root->left) {
TreeNode* tmp = root->right;
delete root;
return tmp;
} else if (!root->right) {
TreeNode* tmp = root->left;
delete root;
return tmp;
} else {
TreeNode* cur = root->right;
while (cur->left) {
cur = cur->left;
}
cur->left = root->left;
cur = root->right;
delete root;
return cur;
}
}
return root;
}
};
以上为对有两个子树的节点的直接删除操作,除此之外由于二叉搜索树的性质,也可以通过使当前节点替换为最左叶子节点的方式,替换并删除节点后返回当前节点。代码如下:
class Solution {
public:
TreeNode* deleteNode(TreeNode* root, int key) {
if (root == nullptr) return nullptr;
if (root->val < key) {
root->right = deleteNode(root->right, key);
} else if (root->val > key) {
root->left = deleteNode(root->left, key);
} else {
if (!root->left && !root->right) {
delete root;
return nullptr;
} else if (!root->left) {
TreeNode* tmp = root->right;
delete root;
return tmp;
} else if (!root->right) {
TreeNode* tmp = root->left;
delete root;
return tmp;
} else {
TreeNode* cur = root->right;
while (cur->left) {
cur = cur->left;
}
// 此处为将root的值改为cur,并执行寻找删除cur的操作
root->val = cur->val;
root->right = deleteNode(root->right, root->val);
}
}
return root;
}
};
原文地址:https://blog.csdn.net/Liaac/article/details/140620385
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!