[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.
The successor of a node p
is the node with the smallest key greater than p.val
.
Example 1:
Input: root = [2,1,3], p = 1
Output: 2
Explanation: 1's in-order successor node is 2. Note that both p and the return value is of TreeNode type.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
TreeNode res = null;
while (root != null) {
if (root.val <= p.val) {
root = root.right;
} else {
// only when p.val < root, root can possibly be the successor, continue check root.left
res = root;
root = root.left;
}
}
return res;
}
}
Good Reference: https://leetcode.com/problems/inorder-successor-in-bst/discuss/72653/Share-my-Java-recursive-solution
[LC] 285. Inorder Successor in BST的更多相关文章
- [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 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 ...
- [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
原题链接在这里:https://leetcode.com/problems/inorder-successor-in-bst/ Given a binary search tree and a nod ...
- [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] 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 ...
- Inorder Successor in BST 解答
Question Given a binary search tree and a node in it, find the in-order successor of that node in th ...
- [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 ...
随机推荐
- Day 7:TreeSet
补充上一日:HashCode方法默认返回的是内存地址,String类已经重写了对象的HashCode方法 方法细节:取出数组中的值或字符串的值按照规定计算返回一个值,如果两个字符串内容一致就会返回相同 ...
- 浅谈无字母数字构造webshell
0x00 问题 <?php include 'flag.php'; if(isset($_GET['code'])){ $code = $_GET['code']; if(strlen($cod ...
- JS实现时间选择器
源代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...
- Java模板引擎之Freemarker 学习笔记 一
什么是Freemarker Freemarker是模板引擎,不是Web框架,只是视图层的组件,官网是 https://freemarker.apache.org/ Freemarker原理 数据模型+ ...
- .NET技术-6.0. Expression 表达式树 生成 Lambda
.NET技术-6.0. Expression 表达式树 生成 Lambda public static event Func<Student, bool> myevent; public ...
- NiFi_Demo_调度示例
1.背景 要求:每天凌晨1:00后开始每2min执行一次sql查询 2.作业编排 3.各模块配置 3.1 GenerateFlowFile 作用:用于产生flowfile,该flowfile内容为空. ...
- Django1.11基础视图
Django视图 路由命名与reverse反解析 在项目urls中的include函数,使用namespace参数定义路由命名空间 url(r'^',incude('book.urls',namesp ...
- 2020年使用Delphi的25个理由(我觉得四个优点:控件+可视化开发+跨平台+数据库,还有一个编译快,运行快)——人生苦短,我用Delphi!
25年后从10个使用Delphi的理由到1个至25个使用Delphi 10.3的理由 25年前发布Delphi 1时,我汇总了使用Delphi的十大理由.这是我精通Delphi原始书的序言中的原始列表 ...
- 编译seastar
1. 下载代码 git clone https://github.com/scylladb/seastar.git cd seastar git submodule update --init --r ...
- 禁止网站F12和查看源码
window.onload=function(){ document.onkeydown=function(){ var e=window.event||arguments[0]; if(e.keyC ...