[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 ...
随机推荐
- 洛谷 P5663 加工零件
题目传送门 解题思路: 最暴力的做法: bfs模拟,每次将一个阶段的所有点拿出来,将其所有直连的点都放进队列,知道本阶段结束,最后看1号点会不会在最后一个阶段被放入队列.(洛谷数据40分) 优化了一下 ...
- java-正则表达式判断移动联通电信手机号
package com.linbilin.phone; import java.util.regex.Matcher; import java.util.regex.Pattern; public c ...
- Delphi7 流操作_压缩
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- 改变UILable里面文字的大小和颜色
UILabel *lb = [[UILabel alloc]init]; NSMutableAttributedString *attriStr = [[NSMutableAttributedStri ...
- json,pickle,shelve序列化
import json a = [{"a":"b"}] jd = json.dumps(a) #序列化,就是对象通过内存能够存储和传输的过程 with open ...
- Bugku 社工
1.密码 姓名:张三 生日:19970315 猜想KEY是:zs19970315. 结果就是如此.
- count(1),count(*)和count(列)的比较
转自:https://www.cnblogs.com/Caucasian/p/7041061.html 1.关于count(1),count(*),和count(列名)的区别 相信大家总是在工作中,或 ...
- py02_04:三元运算法
a if a > b else c # a>b 成立,则为真,如果a>b为假,则返回c
- 面试准备 DOM
基本概念:Dom事件的级别 Dom0 级别 element.onclick=function() {} Dom1 没有制定事件相关的 Dom2 element.addEventListener(&q ...
- mysql字符集配置&mysql中文乱码
问题描述 这两天重置了下自己的电脑系统,一个ubuntu,另外一个当然就是windows. 不过在运行程序的时候发现,出现了很多的"????",也就是乱码字符.毫无疑问,这定然是m ...