Day 22 二叉树
235. 二叉搜索树的最近公共祖先
根据二叉搜索树的性质,相比普通二叉树可以极大程度的简化代码,作为公共祖先其值一定在两个给定节点值之间,从树根往下遍历,第一次出现两个给定节点值之间的值,那个节点即为最近公共祖先(为什么是最近不是最远?根节点一般为最远,第一次出现的值处于两个给定节点值之间的节点为最近)
递归法
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (!root) return nullptr;
if (root->val < p->val && root->val < q->val)
{
return lowestCommonAncestor(root->right, p, q);
}
else if (root->val > p->val && root->val > q->val)
{
return lowestCommonAncestor(root->left, p, q);
}
else return root;
}
};
迭代法
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (p->val > q->val)
{
swap(p, q);
}
TreeNode *cur = root;
while (cur)
{
if (cur->val < p->val)
{
cur = cur->right;
}
else if (cur->val > q->val)
{
cur = cur->left;
}
else
{
return cur;
}
}
return nullptr;
}
};
代码随想录里的解法更加简洁
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
while(root) {
if (root->val > p->val && root->val > q->val) {
root = root->left;
} else if (root->val < p->val && root->val < q->val) {
root = root->right;
} else return root;
}
return NULL;
}
};
701. 二叉搜索树中的插入操作
没有要求平衡再简单不过,直接按搜索的方式遍历到最后一个叶子节点,在叶子节点下添加新的节点就可以了
迭代法
用迭代法简单明了
class Solution {
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
TreeNode *cur = root, *pre = nullptr;
while (cur)
{
pre = cur;
if (cur->val < val)
{
cur = cur->right;
}
else if (cur->val > val)
{
cur = cur->left;
}
}
if (pre)
{
if (pre->val < val)
{
pre->right = new TreeNode(val);
}
else
{
pre->left = new TreeNode(val);
}
return root;
}
else return new TreeNode(val);
}
};
递归法
记住要记录父节点,就有大概的思路了
class Solution {
TreeNode *parent= nullptr;
void traversal(TreeNode *root, int val)
{
if (!root)
{
TreeNode *node = new TreeNode(val);
if (parent->val < val) parent->right = node;
else parent->left = node;
return;
}
parent = root;
if (root->val < val) traversal(root->right, val);
if (root->val > val) traversal(root->left, val);
}
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
if (!root) return new TreeNode(val);
traversal(root, val);
return root;
}
};
450. 删除二叉搜索树中的节点
写麻了,涉及到结构调整,有点难文章来源:https://www.toymoban.com/news/detail-584661.html
class Solution {
public:
TreeNode* deleteNode(TreeNode* root, int key) {
if (!root) return nullptr;
else 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 *ret = root->right;
delete root;
return ret;
}
else if (!root->right)
{
TreeNode *ret = root->left;
delete root;
return ret;
}
else
{
TreeNode *cur = root->right;
while (cur->left)
{
cur = cur->left;
}
cur->left = root->left;
TreeNode *tmp = root;
root = root->right;
delete tmp;
return root;
}
}
return root;
}
};
删除节点太难了,这里就不写迭代了。文章来源地址https://www.toymoban.com/news/detail-584661.html
到了这里,关于算法刷题Day 22 二叉搜索树的最近公共祖先+二叉搜索树中的插入操作+删除二叉搜索树中的节点的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!