235. 二叉搜索树的最近公共祖先
文档讲解:代码随想录.二叉搜索树的最近公共祖先
视频讲解:二叉搜索树找祖先就有点不一样了!| 235. 二叉搜索树的最近公共祖先
状态:已完成
代码实现
/**
* 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 == p || root == q || root == NULL) {
// return root;
// }
// TreeNode* left = lowestCommonAncestor(root->left, p, q);
// TreeNode* right = lowestCommonAncestor(root->right, p, q);
// if (left != NULL && right != NULL) {
// return root;
// } // 这个条件遗漏了
// if (left == NULL && right != NULL) {
// return right;
// } else if (left != NULL && right == NULL) {
// return left;
// } else { // left ==NULL right == NULL
// return NULL;
// }
// 第二种方法,迭代法
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;
}
};
心得体会
- 和236的方法一样
701.二叉搜索树中的插入操作
文档讲解:代码随想录.二叉搜索树中的插入操作
视频讲解:原来这么简单? | LeetCode: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 == 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;
// 第二种方法,迭代法
if (root == nullptr) {
TreeNode* node = new TreeNode(val);
return node;
}
TreeNode* cur = root;
TreeNode* per = root;//都是从第一个节点开始的
while (cur != nullptr) {
per = cur;
if (cur->val > val)
cur = cur->left;
else
cur = cur->right;
}
TreeNode* node = new TreeNode(val);
if (val < per->val)
per->left = node;
else
per->right = node;
return root;
}
};
心得体会
- 递归法和迭代法的理解更加的深入和透彻了
450.删除二叉搜索树中的节点
文档讲解:代码随想录.删除二叉搜索树中的节点
视频讲解:调整二叉树的结构最难!| LeetCode:450.删除二叉搜索树中的节点
状态:已完成
代码实现
/**
* 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 == nullptr) { // 第一种情况,遍历节点始终没找到
return root;
}
if (root->val == key) {
if (root->right == nullptr &&
root->left == nullptr) { // 第二种情况,全部为空的情况
delete root;
return nullptr;
} else if (root->left == nullptr) { // 第三种情况,左子树为空
auto node = root->right;
delete root;
return node;
} else if (root->right == nullptr) { // 第四种情况,右子树为空
auto node = root->left;
delete root;
return node;
} else {
auto cur = root->right;
while (cur->left != nullptr) {
cur = cur->left;
}
cur->left = root->left;
auto temp = root;
root = root->right;
delete temp;
return root;
}
}
if (key < root->val) {
root->left = deleteNode(root->left, key);
} else {
root->right = deleteNode(root->right, key);
}
return root;
}
};
心得体会
- 遇到类似的题解,可以优先分析思路,对可能的中止条件分析完成之后,再进行代码的撰写