235. 二叉搜索树的最近公共祖先
笔记
- 关键在于利用二叉搜索树的性质,对于p,q的最近共祖先m,一定有m的值落在pq之间
C++代码
递归
class Solution {
private:
TreeNode* traversal(TreeNode* cur, TreeNode* p, TreeNode* q) {
if (cur == NULL) return cur;
// 中
if (cur->val > p->val && cur->val > q->val) { // 左
TreeNode* left = traversal(cur->left, p, q);
if (left != NULL) {
return left;
}
}
if (cur->val < p->val && cur->val < q->val) { // 右
TreeNode* right = traversal(cur->right, p, q);
if (right != NULL) {
return right;
}
}
return cur;
}
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
return traversal(root, p, q);
}
};
迭代
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. 二叉搜索树中的插入操作
笔记
- 递归:通过递归函数返回值完成了新加入节点的父子关系赋值操作了,下一层将加入节点返回,本层用root->left或者root->right将其接住。
C++代码
递归
class Solution {
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
if (root == NULL) {
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* r=new TreeNode(val);
return r;
}
TreeNode* pre;
TreeNode* p=root;
while(root){
if(root->val>val){
pre=root;
root=root->left;
}else if(root->val<val){
pre=root;
root=root->right;
}
}
if(pre->val>val){
TreeNode* r=new TreeNode(val);
pre->left=r;
}
if(pre->val<val){
TreeNode* r=new TreeNode(val);
pre->right=r;
}
return p;
}
};
450. 删除二叉搜索树中的节点
笔记
- 递归:跟上题的重点一致,返回值来构建父子节点
- 迭代:1.根据二叉搜索树的性质,可以直接把左子树挂在右子树最左节点2.保存父节点方便删除
C++代码
通用,非二叉搜索树也行
class Solution {
public:
TreeNode* deleteNode(TreeNode* root, int key) {
if(root==nullptr)return root;
if(root->val==key){
if(root->right==nullptr){
return root->left;
}
TreeNode* 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;
}
};
迭代
class Solution {
private:
// 将目标节点(删除节点)的左子树放到 目标节点的右子树的最左面节点的左孩子位置上
// 并返回目标节点右孩子为新的根节点
// 是动画里模拟的过程
TreeNode* deleteOneNode(TreeNode* target) {
if (target == nullptr) return target;
if (target->right == nullptr) return target->left;
TreeNode* cur = target->right;
while (cur->left) {
cur = cur->left;
}
cur->left = target->left;
return target->right;
}
public:
TreeNode* deleteNode(TreeNode* root, int key) {
if (root == nullptr) return root;
TreeNode* cur = root;
TreeNode* pre = nullptr; // 记录cur的父节点,用来删除cur
while (cur) {
if (cur->val == key) break;
pre = cur;
if (cur->val > key) cur = cur->left;
else cur = cur->right;
}
if (pre == nullptr) { // 如果搜索树只有头结点
return deleteOneNode(cur);
}
// pre 要知道是删左孩子还是右孩子
if (pre->left && pre->left->val == key) {
pre->left = deleteOneNode(cur);
}
if (pre->right && pre->right->val == key) {
pre->right = deleteOneNode(cur);
}
return root;
}
};