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.对于某个节点,他的左分支小于自身 ...
随机推荐
- 文件和目录之间建立链接 (ln)
- Mybatis相关知识点(二)
Mybatis解决jdbc编程的问题 1. 数据库连接创建.释放频繁造成系统资源浪费从而影响系统性能,如果使用数据库连接池可解决此问题. 解决:在SqlMapConfig.xml中配置数据连接池,使用 ...
- CAS你知道吗
1.比较并交换 CASDemo /** * CAS => compareAndSet * 比较并交换 */ public class CASDemo { public static void m ...
- C++一元多项式求导
这个题难度不大但是坑有点多,要考虑的点有几个: 1.测试用例为x 0 这个直接输出 0 0即可. 2.注意空格的输出 3.测试点3我好几次都没过,最后参考了别的答案加以修改才通过. 测试点3没过的代码 ...
- SpringIOC原理浅析
1. IoC理论的背景我们都知道,在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最终实现系统的业务逻辑. 图1:软件系统中耦合的对象 如果我们打开机械 ...
- 4.Vue.js-起步
Vue.js 起步 每个 Vue 应用都需要通过实例化 Vue 来实现. 语法格式如下: var vm = new Vue({ // 选项 }) 接下来让我们通过实例来看下 Vue 构造器中需要哪些内 ...
- ssm+ajax实现登陆
ssm的搭建见上一章 1.数据协议层 public User selectByLoginnameAndPassword(@Param("loginname")String logi ...
- Redis5.0.8 Cluster集群部署
目录 一.Redis Cluster简介 二.部署 三.创建主库 一.Redis Cluster简介 Redis Cluster集群是一种去中心化的高可用服务,其内置的sentinel功能可以提供高可 ...
- [V&N2020 公开赛]babybabypwn
写在开头,感谢"影二つ"师傅的指点. 题目的做法思路网上一搜一大把,这篇博客主要记录一下这道题用pwntools写srop的时候,为什么需要省略前面8个字节. 在看题目之前,先来学 ...
- 保留重复项(Power Query 之 M 语言)
数据源: "姓名""基数""个人比例""个人缴纳""公司比例""公司缴纳"&qu ...