注意点:
1.二叉搜索树是有序的,所以应该抓住这个特点
//如果两个节点小于中间节点,则一定在左子树
//如果两个节点大于中间节点,则一定在右子树
2.本题解法与236. 二叉树的最近公共祖先 解法有相似之处
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
TreeNode* travesal(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root == NULL) return root;
//如果两个节点小于中间节点,则一定在左子树--左
if(root->val > p->val && root->val > q->val) {
TreeNode* left = lowestCommonAncestor(root->left, p, q);
if(left != NULL ) return left;//找到了最近的公共祖先
}
//如果两个节点大于中间节点,则一定在右子树--右
if(root->val < p->val && root->val < q->val) {
TreeNode* right = lowestCommonAncestor(root->right, p, q);
if(right != NULL ) return right;
}
//如果一小一大,那么恭喜你,节点就是它--中
return root;
}
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
return travesal(root, p, q);
}
};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
//当遇到空节点的时候,将val创建为一个节点
if(root == nullptr) {
TreeNode* node = new TreeNode(val);
return node;
}
// 如果val大于根节点,则在左子树
if(val < root->val) {
// 递归到最后,得到val创建的那个新节点
TreeNode* left = insertIntoBST(root->left, val);
// 将新节点连接起来
root->left = left;
}
if(val > root->val) {
TreeNode* right = insertIntoBST(root->right, val);
root->right = right;
}
// 返回最终的那个根节点
return root;
}
};
注意点:
1.分五种情况,这五种情况成立有一个大的前提就是root->val == key
以下是五种情况:
1.没有找到节点,返回NULL
2.左右孩子都为空,直接删除节点,返回NULL
3.左孩子为空,右孩子不为空,返回右孩子
4.左孩子不为空,右孩子为空,返回左孩子
5.左右孩子都不为空,将删除节点的左子树放到删除节点的右子树的左孩子的位置,返回的是右子树
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
分为以下5种情况
1.没有找到节点,返回NULL
2.左右孩子都为空,直接删除节点,返回NULL
3.左孩子为空,右孩子不为空,返回右孩子
4.左孩子不为空,右孩子为空,返回左孩子
5.左右孩子都不为空,将删除节点的左子树放到删除节点的右子树的左孩子的位置,返回的是右子树
*/
class Solution {
public:
TreeNode* deleteNode(TreeNode* root, int key) {
// 1.没有找到节点,返回NULL
if(root == nullptr) return nullptr;
if(root->val == key) {
// 2.左右孩子都为空,直接删除节点,返回NULL
if(root->left == nullptr && root->right == nullptr) {
delete root;
return nullptr;
}
// 3.左孩子为空,右孩子不为空,返回右孩子
else if(root->left == nullptr && root->right != nullptr) {
TreeNode* tmp = root->right;
delete root;
return tmp;
}
// 4.左孩子不为空,右孩子为空,返回左孩子
else if(root->left != nullptr && root->right == nullptr) {
TreeNode* tmp = root->left;
delete root;
return tmp;
}
// 5.左右孩子都不为空,将删除节点的左子树放到删除节点的右子树的左孩子的位置,返回的是右子树
else{
TreeNode* cur = root->right;
while(cur->left != nullptr)
cur = cur->left;
cur->left = root->left;
TreeNode* tmp = root;
root = root->right;
delete tmp;
return root;
}
}
if(root->val > key) root->left = deleteNode(root->left, key);
if(root->val < key) root->right = deleteNode(root->right, key);
return root;
}
};