1.二叉搜索树的最近公共祖先
题目链接/文章讲解:代码随想录
代码:递归法
class Solution {
public:
TreeNode* traversal(TreeNode* node,TreeNode* p,TreeNode* q){
if(node == NULL) return NULL;
if(node->val > p->val && node->val > q->val){
TreeNode* left = traversal(node->left,p,q);
if(left){
return left;
}
}
if(node->val < p->val && node->val < q->val){
TreeNode* right = traversal(node->right,p,q);
if(right){
return right;
}
}
return node;
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
return traversal(root,p,q);
}
};
note:老天,我把大小于搞反了
代码:迭代法
/**
* 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 {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
stack<TreeNode*> st;
if(root == NULL) return NULL;
st.push(root);
while(root){
if(p->val < root->val && q->val < root->val){
root = root->left;
}else if(p->val > root->val && q->val > root->val){
root = root->right;
}else{
return root;
}
}
return NULL;
}
};
2.二叉搜索树中的插入操作
题目链接/文章讲解:代码随想录
代码:
/**
* 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) {
if(root == NULL){
TreeNode* node = new TreeNode(val);
return node;
}
if(val < root->val){
root->left = insertIntoBST(root->left,val);
}
if(val > root->val){
root->right = insertIntoBST(root->right,val);
}
return root;
}
};
note:就正常遍历二叉搜索树,最后遍历到哪里就把新结点插到哪里。
代码:迭代法
class Solution {
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
if(root == NULL){
TreeNode* node = new TreeNode(val);
return node;
}
TreeNode* cur = root;
TreeNode* parent = root;
while(cur){
parent = cur;
if(val < cur->val){
cur = cur->left;
}else if(val > cur->val){
cur = cur->right;
}
}
TreeNode* node = new TreeNode(val);
if(val < parent->val){
parent->left = node;
}else{
parent->right = node;
}
return root;
}
};
note:需要找到合适的parent结点进行插入,因此我们定义了parent指针来记录cur的上一次的值。
3.删除二叉搜索树中的结点
题目链接/文章讲解:代码随想录
代码:递归法
/**
* 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* deleteNode(TreeNode* root, int key) {
// 情况1:找不到要删除的结点
if(root == NULL) return NULL;
// 中
if(root->val == key){
// 情况2:左右子树都为空
if(root->left == NULL && root->right == NULL){
return NULL;
}
// 情况3:右子树为空
if(root->left != NULL && root->right == NULL){
return root->left;
}
// 情况4:左子树为空
if(root->left == NULL && root->right != NULL){
return root->right;
}
// 情况5:左右子树都不为空
TreeNode* cur = root->right;
while(cur->left){
cur = cur->left;
}
cur->left = root->left;
TreeNode* temp = root;
root = root->right;
delete temp;
return root;
}
if(key < root->val){
root->left = deleteNode(root->left,key);
}
if(key > root->val){
root->right = deleteNode(root->right,key);
}
return root;
}
};
note:当待删除结点的左右子树都不为空时,找到其右子树的最左结点,然后让此节点的左子树赋值为待删除结点的左子树,然后返回待删除结点的右子树即可。
普通二叉树的删除: