235. 二叉搜索树的最近公共祖先
TreeNode* lowestCommonAncestor(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;
}
迭代法:
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.二叉搜索树中的插入操作
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* parent;
void traversal(TreeNode* cur, int val) {
if (cur == NULL) {
TreeNode* node = new TreeNode(val);
if (val > parent->val) parent->right = node;
else parent->left = node;
return;
}
parent = cur;
if (cur->val > val) traversal(cur->left, val);
if (cur->val < val) traversal(cur->right, val);
return;
}
TreeNode* insertIntoBST(TreeNode* root, int val) {
//parent = new TreeNode(0);
if(root==nullptr){
root=new TreeNode(val);
}
traversal(root,val);
return root;
}
};
再来看看迭代法,对二叉搜索树迭代写法不熟悉,可以看这篇:二叉树:二叉搜索树登场!(opens new window)
在迭代法遍历的过程中,需要记录一下当前遍历的节点的父节点,这样才能做插入节点的操作。
在二叉树:搜索树的最小绝对差 (opens new window)和二叉树:我的众数是多少? (opens new window)中,都是用了记录pre和cur两个指针的技巧,本题也是一样的。
在这几题里面都用了双指针法
if (root == NULL) {
TreeNode* node = new TreeNode(val);
return node;
}
TreeNode* cur = root;
TreeNode* parent = root; // 这个很重要,需要记录上一个节点,否则无法赋值新节点
while (cur != NULL) {
parent = cur;
if (cur->val > val) cur = cur->left;
else cur = cur->right;
}
TreeNode* node = new TreeNode(val);
if (val < parent->val) parent->left = node;// 此时是用parent节点的进行赋值
else parent->right = node;
return root;
450.删除二叉搜索树中的节点
class Solution {
public:
TreeNode* deleteNode(TreeNode* root, int key) {
if(root==nullptr) return nullptr;//1.
if(root->val==key){
if(root->left == nullptr && root->right == nullptr)//2
{
///! 内存释放
delete root;
return nullptr;
}
else if(root->left == nullptr && root->right != nullptr){//3.
auto retNode = root->right;
///! 内存释放
delete root;
return retNode;
}
else if(root->left != nullptr && root->right == nullptr)//4
{
auto retNode = root->left;
///! 内存释放
delete root;
return retNode;
}else{
TreeNode* cur = root->right;
while(cur->left != nullptr) cur=cur->left;
cur->left=root->left;// 把要删除的节点(root)左子树放在cur的左孩子的位置
TreeNode* tmp = root; // 把root节点保存一下,下面来删除
root = root->right; // 返回旧root的右孩子作为新root
delete tmp; // 释放节点内存(这里不写也可以,但C++最好手动释放一下吧)
return root;
}
}
if(root->val > key) root->left=deleteNode(root->left,key);
if(root->val < key) root->right=deleteNode(root->right,key);
return root;
}
};
669. 修剪二叉搜索树
TreeNode* trimBST(TreeNode* root, int low, int high) {
if(root==nullptr) return nullptr;
if(root->val < low){
TreeNode* right=trimBST(root->right,low,high);// 寻找符合区间[low, high]的节点
return right;
}
if(root->val > high){
TreeNode* left=trimBST(root->left,low,high);// 寻找符合区间[low, high]的节点
return left;
}
root->left = trimBST(root->left,low,high);// root->left接入符合条件的左孩子
root->right=trimBST(root->right,low,high);// root->left接入符合条件的左孩子
return root;
}
108.将有序数组转换为二叉搜索树
class Solution {
public:
TreeNode* traversal(vector<int>& nums, int left, int right){
if(left >right) return nullptr;
int mid=left+(right-left)/2;//的写法相当于是如果数组长度为偶数,中间位置有两个元素,取靠左边的。
TreeNode* root=new TreeNode(nums[mid]);
root->left=traversal(nums,left,mid-1);
root->right=traversal(nums,mid+1,right);
return root;
}
TreeNode* sortedArrayToBST(vector<int>& nums) {
TreeNode* root=traversal(nums,0,nums.size()-1);
return root;
}
};
538.把二叉搜索树转换为累加树
class Solution {
public:
int pre=0;// 记录前一个节点的数值
TreeNode* convertBST(TreeNode* root) {// 右中左遍历
if(root==nullptr) return root;
convertBST(root->right); 右中左
root->val+=pre;// 中
pre=root->val;
convertBST(root->left);// 左
return root;
}
};