LeetCode Inorder Successor in BST
原题链接在这里:https://leetcode.com/problems/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.
Note: If the given node has no in-order successor in the tree, return null.
题解:
successor could be自己的parent, or 右子树中最左的点.
For both cases, if p.val < root.val, update successor as root. Otherwise, root needs to be bigger, root = root.right.
Time Complexity: O(h).
Space: O(1).
AC Java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
if(root == null || p == null){
return null;
}
TreeNode successor = null;
while(root != null){
if(p.val < root.val){
successor = root;
root = root.left;
}else{
root = root.right;
}
}
return successor;
}
}
跟上Inorder Successor in BST II.
LeetCode Inorder Successor in 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 ...
- [Locked] Inorder Successor in BST
Inorder Successor in BST Given a binary search tree and a node in it, find the in-order successor of ...
- LeetCode 510. Inorder Successor in BST II
原题链接在这里:https://leetcode.com/problems/inorder-successor-in-bst-ii/ 题目: Given a binary search tree an ...
- 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. 本题 ...
- 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 ...
- [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 ...
- [LC] 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 ...
随机推荐
- Metronic_下拉列表Select2插件的使用
这个插件是基于Select的扩展插件,能够提供更加丰富的功能和用户体验,它的github官网地址为:https://select2.github.io/,具体的使用案例,可以参考地址:https:// ...
- shell 之for [转]
本文也即<Learning the bash Shell>3rd Edition的第五章Flow Control之读书笔记之二,但我们将不限于此.flow control是任何编程语言中很 ...
- 微博开发平台java SDK demo学习之examples(demo)
本文介绍demo中函数的功能分块 账号 评论 收藏 关系/好友分组 地理信息 OAuth2(开发指南) 位置服务(开发指南)
- 2. MySQL
安装: apt-get install mysql-server 配置: mysql_install_db mysql_secure_installation 另有两处需要修改 [mysqld ...
- Editplus从下载到使用
☆ 准备工作 1,保证浏览器正常上网 2,能下载软件或已经下载到Editplus这个工具. ☆ 下载editplus 在浏览器输入http://www.editplus.com,然后回车.进入edit ...
- Python装饰器与面向切面编程
今天来讨论一下装饰器.装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志.性能测试.事务处理等.装饰器是解决这类问题的绝佳设计,有了装饰器,我们就可以抽离出大量函数中与函数 ...
- swift 子类继承父类
// 子类的指定构造方法必须调用父类构造方法,并确保调用发生在子类存储属性初始化之后.而且指定构造方法不能调用同一个类中的其他指定构造方法: // 便利构造方法必须调用同一个类中的其他指定构造方法(可 ...
- 使用国内镜像源来加速python pypi包的安装
pipy国内镜像目前有: http://pypi.douban.com/ 豆瓣 http://pypi.mirrors.ustc.edu.cn/ 中国科学技术大学 安装时,使用-i参数 pip i ...
- 【HAPPY FOREST】用Unreal Engine4绘制实时CG影像
用Unreal Engine绘制实时CG影像 近年来,对实时CG的关心热度越来越高,但要想弥补与预渲染方式的差异并不是那么容易.这里就有影像业界的先锋进行挑战的MARZA ANIMATION PLAN ...
- Unity 中 使用c#线程
使用条件 天下没有免费的午餐,在我使用unity的那一刻,我就感觉到不自在,因为开源所以不知道底层实现,如果只是简单的做点简单游戏,那就无所谓的了,但真正用到实际地方的时候,就会发现一个挨着一个坑 ...