Leetcode235. 二叉搜索树的最近公共祖先
文章链接:代码随想录
题目链接:235. 二叉搜索树的最近公共祖先
思路:用 236. 二叉树的最近公共祖先 的普通方法就可以AC,或者利用二叉树性质递归或者迭代都很简单。
递归法:
/**
* 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) {
if (root->val > p-> val && root->val > q->val){
TreeNode* l = lowestCommonAncestor(root->left, p, q);
return l;
}
if (root->val < p->val && root->val < q->val){
TreeNode* r = lowestCommonAncestor(root->right, p, q);
return r;
}
return root;
}
};
迭代法:
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;
}
};
Leetcode701.二叉搜索树中的插入操作
文章链接:代码随想录
题目链接:701.二叉搜索树中的插入操作
思路:不用考虑改变树的结构,利用二叉树搜索,插到叶子节点处即可。
递归法:
/**
* 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 (root->val > val) root->left = insertIntoBST(root->left, val);
if (root->val < val) root->right = insertIntoBST(root->right, val);
return root;
}
};
迭代法:注意迭代法不能用root直接遍历了,因为最后要返回root,新定义一个cur。
class Solution {
public:
TreeNode* insertIntoBST(TreeNode* root, int val) {
if (root == NULL){
TreeNode* node = new TreeNode(val);
return node;
}
TreeNode* pre;
TreeNode* cur = root;
while (cur){
pre = cur;
if (cur->val > val) cur = cur->left;
else if (cur->val < val) cur = cur->right;
}
TreeNode* node = new TreeNode(val);
if (pre->val > val) pre->left = node;
if (pre->val < val) pre->right = node;
return root;
}
};
Leetcode450.删除二叉搜索树中的节点
文章链接:代码随想录
题目链接:450.删除二叉搜索树中的节点
思路:利用递归,需要考虑五种情况,另外C++记得手动释放内存。
未释放内存:
/**
* 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) {
if (root == NULL) return root;
if (root->val == key){
if (!root->left && !root->right) {
delete root;
return NULL;
}
else if (!root->left) return root->right;
else if (!root->right) return root->left;
else {
TreeNode* node = root->right;
while (node->left != NULL){
node = node->left;
}
node->left = root->left;
return root->right;
}
}
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 == NULL) return root;
if (root->val == key){
if (!root->left && !root->right) {
delete root;
return NULL;
}
else if (!root->left) {
TreeNode* tmp = root;
root = root->right;
delete tmp;
return root;
}
else if (!root->right) {
TreeNode* tmp = root;
root = root->left;
delete tmp;
return root;
}
else {
TreeNode* node = root->right;
while (node->left != NULL){
node = node->left;
}
node->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;
}
};
第二十二天打卡,这两天在做项目实验,冻冰快拍照片,哈尔滨冬天真冷啊!
加油!!!