二叉搜索树中的顺序后继:从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,这道题和上面一道题是一样的,唯一的区别是节点并非普通的二叉树节点,还带有父节点指针。

循环实现:分析得出:

  1. 如果有右子树:则后继节点是右子的最左值。
  2. 否则,向上找。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的中序后继的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [LeetCode]230. 二叉搜索树中第K小的元素(BST)(中序遍历)、530. 二叉搜索树的最小绝对差(BST)(中序遍历)

    题目230. 二叉搜索树中第K小的元素 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 题解 中序遍历BST,得到有序序列,返回有序序列的k-1号元素. 代 ...

  5. [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 ...

  6. 4.6---找二叉树中序后继(CC150)

    因为,没有重复值,所以只需要做一个标记就OK了. public class Successor { static boolean flag = false; static int result = 0 ...

  7. leetcode 94二叉树的中序遍历

    递归算法C++代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...

  8. 区间dp——cf1025D二叉搜索树的中序遍历好题!

    这题帮我复习了一下BST的中序遍历.. 因为给定的数组是递增的,那么BST的中序遍历一定是1 2 3 4 5 6 7 8 9 ... n 即[l,r]为左子树,那么根节点就是r+1,反之根节点就是l- ...

  9. 数据结构学习-BST二叉查找树 : 插入、删除、中序遍历、前序遍历、后序遍历、广度遍历、绘图

    二叉查找树(Binary Search Tree) 是一种树形的存储数据的结构 如图所示,它具有的特点是: 1.具有一个根节点 2.每个节点可能有0.1.2个分支 3.对于某个节点,他的左分支小于自身 ...

随机推荐

  1. idea数据库报错java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

    通过idea操作数据库,进行数据的增加,运行时报错java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 原因:没有导入mysql-connec ...

  2. Oracle中的null与空字符串''的区别

    含义解释:问:什么是NULL?答:在我们不知道具体有什么数据的时候,也即未知,可以用NULL,我们称它为空,ORACLE中,含有空值的表列长度为零.ORACLE允许任何一种数据类型的字段为空,除了以下 ...

  3. Kotlin 学习(1)

    本文出自链接:https://www.jianshu.com/p/ef9584a8ebf8 Kotlin的插件安装: Settings->Plugins->Browse Repositor ...

  4. Advanced C++ | Virtual Constructor

    Can we make a class constructor virtual in C++ to create polymorphic objects? No. C++ being static t ...

  5. fatal: unable to access 'https://github.com/xxxxx/xxxx.git/': Failed to connect to github.com port 443: Timed out

    今天使用git push的时候提示"fatal: unable to access 'https://github.com/xxxxx/xxxx.git/': Failed to conne ...

  6. spring下春注解的声明式事务控制

    package com.hope.test;import com.hope.domain.Account;import com.hope.service.IAccountService;import ...

  7. Alamofire-5.0.0 以上报错

    摘要 Alamofire 更新到新版本时,遇到了两个错误和一个警告️,所以记录下来它们,以及如何解决它们.给其他出现类似问题的同道一些解决的方向. 今天新开启一个项目,因为网络请求选择 Alamofi ...

  8. python实现skywalking邮件告警webhook接口

    1.介绍 Skywalking可以对链路追踪到数据进行告警规则配置,例如响应时间.响应百分比等.发送警告通过调用webhook接口完成.webhook接口用户可以自定义. 2.默认告警规则 告警配置文 ...

  9. 使用MyBatis框架时发现的一些小bug

    在大配置MyBatis.xml中:  不能有空节点属性 ,否则启动服务器后点击登录没有反应. 异常问题: ause: java.sql.SQLException: Value '0000-00-00 ...

  10. CSS伪类选择器实现三角形

    使用css实现常用的三角效果 项目中三角: .breadcrumb{ height: 40px; line-height: 40px; padding: 0 20px; border-top: 1px ...