LeetCode 285. 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:
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 public class Solution { 11 public TreeNode inorderSuccessor(TreeNode root, TreeNode p) { 12 if(root == null || p == null){ 13 return null; 14 } 15 TreeNode successor = null; 16 while(root != null){ 17 if(p.val < root.val){ 18 successor = root; 19 root = root.left; 20 }else{ 21 root = root.right; 22 } 23 } 24 return successor; 25 } 26 }
跟上Inorder Successor in BST II.
转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/5206006.html
LeetCode 285. 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 449. Serialize and Deserialize BST | 449. 序列化和反序列化二叉搜索树(BST后序遍历性质)
题目 https://leetcode.com/problems/serialize-and-deserialize-bst/ 题解 本题的难点在于 利用 BST 的性质. 几个提示 根据后序遍历BS ...
- 两种方法 Find inorder succ of BST
方法一: 如果node的定义有parent reference 就非常直接: public static BTNode inorderSucc(BTNode e) { if (e != null) { ...
- LeetCode 285. 二叉搜索树中的顺序后继(中序遍历)
文章目录 1. 题目 2. 解题 1. 题目 给你一个二叉搜索树和其中的某一个结点,请你找出该结点在树中顺序后继的节点. 结点 p 的后继是值比 p.val 大的结点中键值最小的结点. 示例 1: 输 ...
- LeetCode 938. Range Sum of BST 时间复杂度(O(n))
时间复杂度(O(n)), 搜索二叉树树的遍历 # Definition for a binary tree node. # class TreeNode: # def __init__(self, x ...
- Leetcode重点250题
LeetCode重点250题 这个重点题目是把LeetCode前400题进行精简.精简方法如下: 删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于 ...
- leetcode top interview题目补充
这个列表中有78道题目,是leetcode上top interview questions中的.原本是有145道题目,但是部分题目和top 100 liked questions是重复的(见另一篇文章 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...
- LEETCODE-刷题个人笔记 Python(1-400)-TAG标签版本
1. Array (1) 27. Remove Element(Easy) 给定数组nums和值val,在适当位置删除该值的所有实例并返回新长度. 思路: 不需要使用排序,如果等于该值,则将n-1的值 ...
最新文章
- 如何链接两个名字一样动态库
- getdc 与getwindowDc的区别,loadbitmap 与loadimage的区别
- Swoole安装报错No releases available for package pecl.php.net/swoole
- K8s普通用户配置权限解决User “system:anonymous“ cannot list nodes at the cluster scope
- java封装概念_Java面向对象----封装概念
- mysql id 不在集合里面_MySQL,PHP:从表中选择*,其中id不在数组中
- 软件测试的错误优先级,软件测试典型错误
- Git ssh fingerprint不一致
- 【蓝桥杯单片机笔记6】PWM脉宽调制信号
- Leetcode 1484题: Group Sold Products By The Date
- 2022年湖北安全员ABC证书查询是在哪里查真假呢?
- 惠普HP Ink Tank 118 打印机驱动
- RFID标签的安全机制——LCAP协议的C++代码实现
- QQ斗地主终于出S60V5版本了!
- 二、final和static关键字,接口和抽象类
- 【编程之美】一摞烙饼的排序
- Codeforces - Yura and Developers
- Leetcode 1229. 安排会议日程
- evus是什么意思_去美国之前要上网做evus是什么意思
- Python机器学习算法入门教程(四)
热门文章
- DE16 Continuation: More General Periods
- 函数调用关系python_追踪python函数调用关系
- yolo5纸张卡片顶点检测,实现任意倾斜角度较正
- php可逆加密解密函数,php 好用可逆的 加密解密 函数。
- java访问其它服务器_java – 多个客户端同时访问服务器
- quartz定时器依赖_Spring Quartz定时器 配置文件详解
- 怎么得到hid设备名_上海海关旧设备进口清关公司这个不错
- vue页面回显数据_解决vue表单回显数据无法修改的问题
- Linux网络处理“零拷贝”技术mmap()内核进程间通信设计8086分页管理——摆在一起来谈谈...
- python画笔的尺寸_Matplotlib 常用画图命令总结:使用 Python 在论文中画出一手漂亮的数据图...