BST的中序后继
二叉搜索树中的顺序后继:从BST中找到指定节点的下一个节点。

比如1的下一个是2,2的下一个是3,4的下一个是5.
思路:
方法1:递归执行中序遍历,获取list,得到p的下一个。时间O(N),空间O(N)
方法2:
递归执行中序遍历,在递归过程中获取x的下一个。如果当前值是<=x的,那么根据BST的特性只需要在右子树中找。如果当前值>x,则当前值有可能,它的左子树也有可能有更小的但是也>x的,对左子递归后,选择更接近的(更小的).
时间O(logN),空间O(logN)调用栈的深度。
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
if(p==null||root==null){
return null;
}
if(root.val<=p.val){//当前和左边都不可能>p
return inorderSuccessor(root.right,p);
}
//root>p
TreeNode res1=inorderSuccessor(root.left,p);
if(res1!=null&&res1.val<root.val){
return res1;
}else{
return root;
}
}
方法3:循环实现
如果当前值是<=x的,那么根据BST的特性只需要在右子树中找:cur=cur.right。
如果当前值>x,则当前值有可能,它的左子树也有可能有更小的但是也>x的。则每次走入这个分支时,当前点是一个候选点,记录该节点的值和历史最小节点的值。
时间O(logN),空间O(1)
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
if(p==null||root==null){
return null;
}
TreeNode cur=root;
TreeNode res=null;
while(cur!=null){
if(cur.val<=p.val){
cur=cur.right;
}else{
if(res==null||res.val>cur.val){
res=cur;
}
cur=cur.left;
}
}
return res;
}
二叉搜索树中的中序后继 II,这道题和上面一道题是一样的,唯一的区别是节点并非普通的二叉树节点,还带有父节点指针。
循环实现:分析得出:
- 如果有右子树:则后继节点是右子的最左值。
- 否则,向上找。cur作为左子时,对于的父节点是第一个>x的值。
public Node inorderSuccessor(Node x) {
if(x==null){
return null;
}
if(x.right!=null){
Node tmp=x.right;
while(tmp.left!=null){
tmp=tmp.left;
}
return tmp;
}else{
Node cur=x;
while(cur.parent!=null&&cur!=cur.parent.left){
cur=cur.parent;
}
//cur为null、cur的parent为null
return cur.parent;
}
}
BST的中序后继的更多相关文章
- [LeetCode] Inorder Successor in BST 二叉搜索树中的中序后继节点
Given a binary search tree and a node in it, find the in-order successor of that node in the BST. No ...
- [LeetCode] Inorder Successor in BST II 二叉搜索树中的中序后继节点之二
Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Th ...
- [LeetCode] 285. Inorder Successor in BST 二叉搜索树中的中序后继节点
Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Th ...
- [LeetCode]230. 二叉搜索树中第K小的元素(BST)(中序遍历)、530. 二叉搜索树的最小绝对差(BST)(中序遍历)
题目230. 二叉搜索树中第K小的元素 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 题解 中序遍历BST,得到有序序列,返回有序序列的k-1号元素. 代 ...
- [Swift]LeetCode285. 二叉搜索树中的中序后继节点 $ Inorder Successor in BST
Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Th ...
- 4.6---找二叉树中序后继(CC150)
因为,没有重复值,所以只需要做一个标记就OK了. public class Successor { static boolean flag = false; static int result = 0 ...
- leetcode 94二叉树的中序遍历
递归算法C++代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...
- 区间dp——cf1025D二叉搜索树的中序遍历好题!
这题帮我复习了一下BST的中序遍历.. 因为给定的数组是递增的,那么BST的中序遍历一定是1 2 3 4 5 6 7 8 9 ... n 即[l,r]为左子树,那么根节点就是r+1,反之根节点就是l- ...
- 数据结构学习-BST二叉查找树 : 插入、删除、中序遍历、前序遍历、后序遍历、广度遍历、绘图
二叉查找树(Binary Search Tree) 是一种树形的存储数据的结构 如图所示,它具有的特点是: 1.具有一个根节点 2.每个节点可能有0.1.2个分支 3.对于某个节点,他的左分支小于自身 ...
随机推荐
- C++类虚函数内存分布(这个 你必须懂)
转自:http://www.cnblogs.com/jerry19880126/p/3616999.html C++类内存分布 书上类继承相关章节到这里就结束了,这里不妨说下C++内存分布结构,我们来 ...
- mac系统升级导致VirtualBox报Kernel driver not installed (rc=-1908)
一.背景 最近将我的Mac升级成了Monterey版本,结果发现之前的安装的VirtualBox虚拟机无法启动,报了如下错误. Kernel driver not installed (rc=-190 ...
- 【玩具】获取B站视频的音频片段
事情是这样的,我有个和社畜的社会地位不太相符的小爱好--听音乐剧. 基本上是在B站上点开视频听,不是不想在网易云或者QQ音乐听,只是在这些音乐软件上面,我想听的片段要不就收费,要不版本不是我喜欢的,要 ...
- 关于写SpringBoot+Mybatisplus+Shiro项目的经验分享三:问题2
框架: SpringBoot+Mybatisplus+Shiro 简单介绍:关于写SpringBoot+Mybatisplus+Shiro项目的经验分享一:简单介绍 搜索框是该项目重要的一环,由于涉及 ...
- Flannel 启动报错
[root@ ~]#: kubectl logs -f kube-flannel-plcbl -n kube-system kube-flannel I0601 16:58:55.456862 1 m ...
- 日常Java测试第一段 2021/11/12
课堂测试一 package word_show;import java.io.BufferedReader;import java.io.FileNotFoundException;import ja ...
- 18. MYSQL 字符编码配置
MYSQL 5.7版本的my.ini 在C盘隐藏文件夹下 C:\ProgramData\MySQL\MySQL Server 5.7 [client] default-character-set=ut ...
- 【leetcode】565. Array Nesting
You are given an integer array nums of length n where nums is a permutation of the numbers in the ra ...
- Hibernate持久化标志符生成策略
generator子元素定义持久化标识符的生成策略,为持久化类对应的数据库表的主键找到了赋值方法,HIbernate默认将使用assigned的持久化标识符生成策略.关系型数据库的主键定义方式:(1) ...
- size_type 和 size_t 的区别
标准库string里面有个函数size,用来返回字符串中的字符个数,具体用法如下:string st("The expense of spirit\n");cout << ...