235.二叉搜索树的最近公共祖先
题目描述
给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
例如,给定如下二叉搜索树: root = [6,2,8,0,4,7,9,null,null,3,5]
示例1:
输入:root=[6,2,8,0,4,7,9,null,null,3,5],p=2,q=8root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8root=[6,2,8,0,4,7,9,null,null,3,5],p=2,q=8
输出:666
示例2:
输入:root=[6,2,8,0,4,7,9,null,null,3,5],p=2,q=4root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4root=[6,2,8,0,4,7,9,null,null,3,5],p=2,q=4
输出:222
说明:
所有节点的值都是唯一的。
p、q 为不同节点且均存在于给定的二叉搜索树中。
思路
本题的本质也是一个搜索的过程,因为题目中已经规定,p和q均存在于给定的二叉搜索树中,所以要找到最近的公共祖先,其实就是要找到这个两个节点,然后根据这两个节点的结构位置来判断其公共祖先。
解法1
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
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;
}
}
解法2
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
while(true){
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{
break;
}
}
return root;
}
}
总结
递归和迭代方法均可,本质都是一样的。但不得不说第一时间想到的肯定是递归的。
701.二叉搜索树中的插入操作
题目描述
给定二叉搜索树(BST)的根节点 root 和要插入树中的值 value ,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 输入数据 保证 ,新值和原始二叉搜索树中的任意节点值都不同。
注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。 你可以返回 任意有效的结果 。
示例1:
输入:root=[4,2,7,1,3],val=5root = [4,2,7,1,3], val = 5root=[4,2,7,1,3],val=5
输出:[4,2,7,1,3,5][4,2,7,1,3,5][4,2,7,1,3,5]
示例2:
输入:root=[4,2,7,1,3,null,null,null,null,null,null],val=5root = [4,2,7,1,3,null,null,null,null,null,null], val = 5root=[4,2,7,1,3,null,null,null,null,null,null],val=5
输出:[4,2,7,1,3,5][4,2,7,1,3,5][4,2,7,1,3,5]
示例3:
输入:root=[40,20,60,10,30,50,70],val=25root = [40,20,60,10,30,50,70], val = 25root=[40,20,60,10,30,50,70],val=25
输出:[40,20,60,10,30,50,70,null,null,25][40,20,60,10,30,50,70,null,null,25][40,20,60,10,30,50,70,null,null,25]
思路
谁懂啊,看完题目立刻深觉不会,立刻打开答案,看完答案,啊,这好像也不难。
看完答案之后的思路:遍历,找到二叉搜索树对应的为空的位置,然后插入。啊,要被自己蠢哭了。
解法1
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode insertIntoBST(TreeNode root, int val) {
if(root == null) return new TreeNode(val);
if(root.val < val) {
root.right = insertIntoBST(root.right,val);
}
if(root.val > val){
root.left = insertIntoBST(root.left,val);
}
return root;
}
}
解法2
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode insertIntoBST(TreeNode root, int val) {
if(root == null) return new TreeNode(val);
TreeNode newRoot = root;
TreeNode pre = root;
while(root!= null){
pre = root;
if(root.val > val){
root = root.left;
}
else if(root.val < val){
root = root.right;
}
}
if(pre.val > val){
pre.left = new TreeNode(val);
}else{
pre.right = new TreeNode(val);
}
return newRoot;
}
}
总结
可以多看看多练练,没想到说明还不熟悉啊。
450.删除二叉搜索树中的节点
题目描述
给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。
一般来说,删除节点可分为两个步骤:
首先找到需要删除的节点;
如果找到了,删除它。
示例1:
输入:root=[5,3,6,2,4,null,7],key=3root = [5,3,6,2,4,null,7], key = 3root=[5,3,6,2,4,null,7],key=3
输出:[5,4,6,2,null,null,7][5,4,6,2,null,null,7][5,4,6,2,null,null,7]
示例2:
输入:root=[5,3,6,2,4,null,7],key=0root = [5,3,6,2,4,null,7], key = 0root=[5,3,6,2,4,null,7],key=0
输出:[5,3,6,2,4,null,7][5,3,6,2,4,null,7][5,3,6,2,4,null,7]
示例3:
输入:root=[],key=0root = [], key = 0root=[],key=0
输出:[][][]
思路
有了上面插入的题,其实很容易能想到可以分类讨论。but,分类之后的删除后节点调整,完全不会。是我愚钝了。看答案。
解法1
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
root = delete(root,key);
return root;
}
public TreeNode delete(TreeNode root,int key){
if(root == null) return null;
if(root.val > key){
root.left = delete(root.left,key);
}
else if(root.val < key){
root.right = delete(root.right,key);
}
else{
if(root.left == null) return root.right;
if(root.right == null) return root.left;
TreeNode tmp = root.right;
while(tmp.left != null){
tmp = tmp.left;
}
root.val = tmp.val;
root.right = delete(root.right,tmp.val);
}
return root;
}
}
解法2
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
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;
}
}
总结
臣做不到啊,会再看看的,这个节点调整以前没写过。
文章介绍了如何在二叉搜索树中找到两个指定节点的最近公共祖先,以及如何进行插入和删除操作。提供了两种不同的解法,一种是递归,另一种是迭代,强调了解决这类问题的关键在于理解二叉搜索树的特性。
2207

被折叠的 条评论
为什么被折叠?



