二叉搜索树中的顺序后继:从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. 13.Merge k Sorted Lists

    思路:利用map<int,vector<ListNode*> > 做值和指针的映射,最后将指针按值依次链接起来, 时间复杂度O(N),空间O(N) Merge k sorted ...

  2. Python | 迭代器与zip的一些细节

    首先抛出一个困扰本人许久的问题: nums = [1,2,3,4,5,6] numsIter = iter(nums) for _ in zip(*[numsIter]*3): print(_) pr ...

  3. 巩固javaweb第一天

    巩固内容: 实例解析 <!DOCTYPE html> 声明为 HTML5 文档 <html> 元素是 HTML 页面的根元素 <head> 元素包含了文档的元(me ...

  4. Scala(五)【集合的高级使用】

    目录 一.集合属性 size length contains mkString 二.集合方法 drop.dropRight distinct:去重 head.last:获取第一个.最后一个元素 tai ...

  5. 【leetcode】1293 .Shortest Path in a Grid with Obstacles

    You are given an m x n integer matrix grid where each cell is either 0 (empty) or 1 (obstacle). You ...

  6. Shell学习(八)——dd命令

    一.dd命令的解释 dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. 注意:指定数字的地方若以下列字符结尾,则乘以相应的数字:b=512:c=1:k=1024:w=2 参数注释: 1. ...

  7. Linux—禁止用户SSH登录方法总结

    Linux-禁止用户SSH登录方法总结 一.禁止用户登录 1.修改用户配置文件/etc/shadow       将第二栏设置为"*",如下.那么该用户就无法登录.但是使用这种方式 ...

  8. Oracle中分割逗号函数REGEXP_SUBSTR

    最近优化FORM中的查询条件遇到某个字段可以选取多个值的问题,思路当然就是选取时将多个值通过某个符号拼接起来,查询数据的时候将拼接后的字符串按照符号分割开,在分割逗号的时候用到了一个新的方法REGEX ...

  9. Linux服务器---论坛discuz

    Discus Discuz是一款免费的论坛管理系统,大量的使用了AJAX,内部有丰富的模板风格. 1.下载discuz软件(https://cn.wordpress.org/download/rele ...

  10. Linux安装软件出错

    1.Delta RPMs disabled because /usr/bin/applydeltarpm not installed. yum provides '*/applydeltarpm' # ...