"学要有所获。"
任务日期:6.25
题目一链接:235. 二叉搜索树的最近公共祖先 - 力扣(LeetCode)
思路:根据二叉搜索树的性质:如果当前节点大于p,q那么答案就在当前节点的左子树上,同理推另一种情况。
代码:
/**
* 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* traversal(TreeNode* cur,TreeNode* p,TreeNode* q) {
//递归终止条件
if(cur == NULL) return NULL;
//确定单层递归逻辑
//左:根据二叉搜索树的性质来写左右子树
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;
}
//中:
//找到的第一个大于q小于p节点就是二者的最近公共祖先
return cur;
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
return traversal(root,p,q);
}
};
难点:
题目二链接:
思路:当递归到空节点时,就定义一个新节点并将新节点返回给上一个·递归函数,然后进行添加节点的操作,最后返回到中节点时返回root(不用改二叉树的结构)。
代码:
/**
* 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(val < root -> val) {
root -> left = insertIntoBST(root -> left,val);//一直递归一直到节点为空,空节点会返回一个值给这个insert函数
}
//右:
if(val > root -> val) {
root -> right = insertIntoBST(root -> right,val);
}
//中:
return root;
}
};
难点:
题目三链接:450. 删除二叉搜索树中的节点 - 力扣(LeetCode)
思路:还是按照递归三部曲来做,本题特殊点在于本体的终止条件是当root的val值等于key或者root本身是null的时候,在终止条件中分为没找到就是null的情况和找到了两种情况,在找到的情况下有四种情况;然后接着进入单层递归逻辑环节:向左和向右,并对root的左右节点进行赋值
代码:
/**
* 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 nullptr;
//找到了
if(root -> val == key) {
//1.节点是叶子节点:
if(root -> left == nullptr && root -> right == nullptr) return nullptr;
//2.节点是左不空右空:
if(root -> left != nullptr && root -> right == nullptr) return root -> left;
//3.节点左空右不空:
if(root -> left == nullptr && root -> right != nullptr) return root -> right;
//4.节点左右都不是空:
if(root -> left != nullptr && root -> right != nullptr) {
TreeNode* cur = root -> right;//找右子树最左边的点
while(cur -> left != nullptr) cur = cur -> left;//注意判断条件是cur -> left 不是空,最后cur指向某个叶子节点,因为这个节点是比他大的最小的点,符合要求
cur -> left = root -> left;
return root -> right;
}
}
//确定单层递归逻辑
//左:向左递归
if(key < root -> val) root -> left = deleteNode(root -> left,key);//向左递归如果找到key那么就让root -> left等于递归的返回值。
//右:向右递归
if(key > root -> val) root -> right = deleteNode(root -> right,key);
//中:
return root;
}
};
难点:
解释细节3:代码中有解释