二叉搜索树的最近公共祖先 leetcode 235
当前根节点的val如果在q和p的val之间的话,那么当前根节点就是它们的最近公共祖先。如果q和p的val都大于当前根节点的val,那么遍历根节点的右子树,如果q和p的val都小于当前根节点的val,那么遍历根节点的左子树。
解法一:递归
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root==NULL) return root;
if(root->val<p->val&&root->val<q->val){
TreeNode* right=lowestCommonAncestor(root->right,p,q);
if(right) return right;
}
if(root->val>p->val&&root->val>q->val){
TreeNode* left=lowestCommonAncestor(root->left,p,q);
if(left) return left;
}
return root;
}
};
解法二:迭代
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root==NULL) return root;
while(root){
if(root->val<p->val&&root->val<q->val) root=root->right;
else if(root->val>p->val&&root->val>q->val) root=root->left;
else break;
}
return root;
}
};
二叉搜索树中的插入操作 leetcode 701
在二叉搜索树中插入节点,就是插入到某一叶子节点的左孩子或者右孩子。
解法一:递归
class Solution {
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
if(root==nullptr){
TreeNode* node=new TreeNode(val);
return node;
}
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) {
if(root==nullptr){
TreeNode* node=new TreeNode(val);
return node;
}
TreeNode* cur=root;
TreeNode* pre=root;
while(cur!=nullptr){
pre=cur;
if(cur->val<val) cur=cur->right;
else if(cur->val>val) cur=cur->left;
}
TreeNode* node=new TreeNode(val);
if(pre->val<val) pre->right=node;
else if(pre->val>val) pre->left=node;
return root;
}
};
while循环结束之后,pre指向叶子节点。
出现的错误
更新cur和最后链接节点时,应该采用if-else,或者if-else if语句,我一开始是这样写的:
if(cur->val<val) cur=cur->right;
if(cur->val>val) cur=cur->left;
这样写如果第一个条件为真,对cur更新之和,有可能也符合第二个if,出现错误。
删除二叉搜索树中的节点 leetcode 450
一共五种情况:
1.没有找到要删除的节点。
2.删除的节点为叶子节点,左右都为空。
3.删除的节点左为空,右不为空。
4.删除的节点右为空,左不为空。
5.删除的节点左右都不为空。则将删除节点的左子树头结点(左孩子)放到删除节点的右子树的最左面节点的左孩子上。
解法一:根据二叉搜索树特性迭代
class Solution {
public:
TreeNode* deleteNode(TreeNode* root, int key) {
if(!root) return root;
if(root->val==key){
if(!root->left&&!root->right) {
delete root;
return nullptr;
}
else if(!root->left&&root->right){
TreeNode* tmp=root->right;
delete root;
return tmp;
}
else if(root->left&&!root->right){
auto tmp=root->left;
delete root;
return tmp;
}
else{
TreeNode* cur=root->right;
while(cur->left){
cur=cur->left;
}
cur->left=root->left;
auto tmp=root->right;
delete root;
return tmp;
}
}
if(root->val>key) root->left=deleteNode(root->left,key);
if(root->val<key) root->right=deleteNode(root->right,key);
return root;
}
};
注意释放内存!
解法二:普通二叉树迭代
class Solution {
public:
TreeNode* deleteNode(TreeNode* root, int key) {
if(root==nullptr) return root;
if(root->val==key){
if(!root->right){
auto tmp=root->left;
delete root;
return tmp;
}else{
auto cur=root->right;
while(cur->left){
cur=cur->left;
}
swap(cur->val,root->val);
}
}
root->left=deleteNode(root->left,key);
root->right=deleteNode(root->right,key);
return root;
}
};
遍历整个二叉树的节点搜寻目标值,找到目标节点如果其右孩子为空,则直接返回左孩子,如果右孩子不为空,就将右孩子最左边的叶子节点与目标节点的val值交换,然后继续迭代,当再次找到目标值时目标节点肯定是叶子节点,然后返回其左孩子也就是null,删除节点。
这样操作与解法一得到的二叉树会有一些不同,但都符合二叉搜素树。有点神奇!
解法三:迭代
class Solution {
public:
TreeNode* deleteone(TreeNode* target){
if(!target) return target;
if(!target->right){
auto tmp=target->left;
delete target;
return tmp;
}
auto cur=target->right;
while(cur->left){
cur=cur->left;
}
cur->left=target->left;
auto tmp=target->right;
delete target;
return tmp;
}
TreeNode* deleteNode(TreeNode* root, int key) {
if(root==nullptr) return root;
TreeNode* cur=root;
TreeNode* pre=nullptr;
while(cur){
if(cur->val==key) break;
pre=cur;
if(cur->val<key) cur=cur->right;
else cur=cur->left;
}
if(pre==nullptr) return deleteone(cur); //pre没有被赋值,说明只有头节点
if(pre->left&&pre->left->val==key) pre->left=deleteone(cur);
if(pre->right&&pre->right->val==key) pre->right=deleteone(cur);
return root;
}
};
出现的错误
if(pre==nullptr) return deleteone(cur); //pre没有被赋值,说明只有头节点
这里不能直接return nullptr;因为可能删除的节点就是头节点。