题目链接:235. 二叉搜索树的最近公共祖先
简单
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root == nullptr || root == p || root == q) return root;
TreeNode* left = lowestCommonAncestor(root->left, p, q);
TreeNode* right = lowestCommonAncestor(root->right, p, q);
if(left != nullptr && right != nullptr) return root;
else if(left == nullptr && right != nullptr) return right;
else if (left!= nullptr && right == nullptr) return left;
else return nullptr;
}
};
题目链接:701. 二叉搜索树中的插入操作
简单
class Solution {
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
if(!root) return new TreeNode(val);
insertBST(root, val);
return root;
}
void insertBST(TreeNode* curr, int val) {
if(val > curr->val) {
if(curr->right) insertBST(curr->right, val);
else curr->right = new TreeNode(val);
}
else{
if(curr->left) insertBST(curr->left, val);
else curr->left = new TreeNode(val);
}
}
};
题目链接:450. 删除二叉搜索树中的节点
分五种情况:搜索树中没有该节点、待删除节点为叶子节点、待删除节点左子树为空右子树不空、待删除节点右子树为空左子树不空、待删除节点左右子树均不为空。
自己写出一种迭代法:
class Solution {
public:
TreeNode* deleteNode(TreeNode* root, int key) {
if(root == nullptr) return root;
TreeNode* curr = root,* pre = nullptr;
while(curr->val != key){
pre = curr;
if(curr->val > key) curr = curr->left;
else curr = curr->right;
if(curr == nullptr) return root;
}
if(pre != nullptr) {
if(curr->left == nullptr && curr->right == nullptr){
if(pre->left == curr) pre->left = nullptr;
else pre->right = nullptr;
}
else if(curr->left != nullptr && curr->right == nullptr) {
if(pre->left == curr) pre->left = curr->left;
else pre->right = curr->left;
}
else if(curr->left == nullptr) {
if(pre->left == curr) pre->left = curr->right;
else pre->right = curr->right;
}
else {
TreeNode* temp = curr->right;
while(temp->left != nullptr) temp = temp->left;
temp->left = curr->left;
if(pre->left == curr) pre->left = curr->right;
else pre->right = curr->right;
}
delete curr;
return root;
}
else {
if(curr->left == nullptr && curr->right == nullptr) root =nullptr;
else if(curr->left != nullptr && curr->right == nullptr) root = curr->left;
else if(curr->left == nullptr) root = curr->right;
else {
TreeNode* temp = curr->right;
while(temp->left != nullptr) temp = temp->left;
temp->left = curr->left;
root = curr->right;
}
delete curr;
return root;
}
}
};
迭代法中根节点与其他节点删除操作不相同,想到链表章节应对这种情况会加一个虚拟头结点,这里加入一个虚拟根节点。
class Solution {
public:
TreeNode* deleteNode(TreeNode* root, int key) {
if(root == nullptr) return root;
TreeNode* curr = root,* dummy_root = new TreeNode();
dummy_root->left = root;
TreeNode* pre = dummy_root;
while(curr->val != key){
pre = curr;
if(curr->val > key) curr = curr->left;
else curr = curr->right;
if(curr == nullptr) return root;
}
if(curr->left == nullptr && curr->right == nullptr){
if(pre->left == curr) pre->left = nullptr;
else pre->right = nullptr;
}
else if(curr->left != nullptr && curr->right == nullptr) {
if(pre->left == curr) pre->left = curr->left;
else pre->right = curr->left;
}
else if(curr->left == nullptr) {
if(pre->left == curr) pre->left = curr->right;
else pre->right = curr->right;
}
else {
TreeNode* temp = curr->right;
while(temp->left != nullptr) temp = temp->left;
temp->left = curr->left;
if(pre->left == curr) pre->left = curr->right;
else pre->right = curr->right;
}
root = dummy_root->left;
delete dummy_root;
delete curr;
return root;
}
};
后来看了视频写出来递归的方法:
class Solution {
public:
TreeNode* deleteNode(TreeNode* root, int key) {
if(root == nullptr) return nullptr;
else if(root->val == key) {
if(root->left == nullptr && root->right == nullptr) {
delete root;
return nullptr;
}
else if(root->left != nullptr && root->right == nullptr) {
TreeNode* temp = root->left;
delete root;
return temp;
}
else if(root->left == nullptr) {
TreeNode* temp = root->right;
delete root;
return temp;
}
else {
TreeNode* temp = root->left;
while(temp->right != nullptr) temp = temp->right;
temp->right = root->right;
temp = root->left;
delete root;
return temp;
}
}
else if(root->val > key) {
root->left = deleteNode(root->left, key);
return root;
}
else {
root->right = deleteNode(root->right, key);
return root;
}
}
};