235.二叉搜索树的最近公共祖先
思路:想不到怎么利用二叉搜索树的特性,难道说祖先一定是在p 和 q之间的?
尝试
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if( root == null || root == p || root == q ){
return root;
}
TreeNode left = lowestCommonAncestor( root.left, p, q);
TreeNode right = lowestCommonAncestor( root.right, p, q);
if(left == null && right == null){
return root;
}else if(left != null && right == null){
return left;
}else if(left == null && right != null){
return right;
}else{
return root;
}
}
}
🍉【left == null && right == null】要返回null,而不是root
答案
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root.val > p.val && root.val > q.val) return lowestCommonAncestor(root.left, p, q);
if (root.val < p.val && root.val < q.val) return lowestCommonAncestor(root.right, p, q);
return root;
}
}
小结
🍉当我们从上向下去递归遍历,第一次遇到 cur节点是数值在[q, p]区间中,那么cur就是 q和p的最近公共祖先。
🍉root在区间左边,说明root小了,p 和 q在右子树,往右遍历,反之,往左遍历
701.二叉搜索树中的插入操作
题目:701. 二叉搜索树中的插入操作 - 力扣(LeetCode)
思路:根据val和root.val的大小对比,递归进入左右子树,情况有三种,root左右孩子为空,root左孩子空,root右孩子空
尝试(部分AC)
class Solution {
public TreeNode insertIntoBST(TreeNode root, int val) {
if(root == null) return new TreeNode(val);
if(root.left == null || root.right == null){
if(root.val > val) root.left = new TreeNode(val);
if(root.val < val) root.right = new TreeNode(val);
}
if(val > root.val) insertIntoBST(root.right,val);
if(val < root.val) insertIntoBST(root.left,val);
return root;
}
}
答案
class Solution {
public TreeNode insertIntoBST(TreeNode root, int val) {
if (root == null) // 如果当前节点为空,也就意味着val找到了合适的位置,此时创建节点直接返回。
return new TreeNode(val);
if (root.val < val){
root.right = insertIntoBST(root.right, val); // 递归创建右子树
}else if (root.val > val){
root.left = insertIntoBST(root.left, val); // 递归创建左子树
}
return root;
}
}
小结
🍉对题目理解有误,下图这种情况插入【6】,就是把【6】放在【7】的左孩子上,我起初想的是要将【6】放在【4】的右孩子,去替代【7】,不用改变树结构
🍉看完文字提示,思路是对的,但是代码冗余了
450.删除二叉搜索树中的节点
题目:450. 删除二叉搜索树中的节点 - 力扣(LeetCode)
思路:删除的逻辑不会啊,只知道怎么找到节点,但是删除节点涉及到了树结构的修改,就按照是否为叶子节点来分,是之间删除,不是再说
尝试(部分AC)
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
if(root == null) return root;
if(root.val == key){
if(root.left == null && root.right ==null) return null;
if(root.right != null && root.left!=null){
// 对root.left还需要分情况处理,不想改了
root.left.right = root.right;
return root.left;
};
if(root.right != null && root.left == null) return root.right;
if(root.right == null && root.left != null) return root.left;
}
if(root.val > key){
root.left = deleteNode(root.left,key);
}else 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 == null) {
return root.right;
} else if (root.right == null) {
return root.left;
} else {
TreeNode cur = root.right;
while (cur.left != null) {
cur = cur.left;
}
cur.left = root.left;
root = root.right;
return root;
}
}
if (root.val > key) root.left = deleteNode(root.left, key);
if (root.val < key) root.right = deleteNode(root.right, key);
return root;
}
}
小结
🍉二叉搜索树中,左子树的值,一定是小于右子树的,所以搜索到节点的有两个孩子时,要不停深入右孩子的左侧,直到空节点,把左孩子插进去,while循环的作用就是这个!
TreeNode cur = root.right;
while (cur.left != null) {
cur = cur.left;
}
cur.left = root.left;
root = root.right;
return root;