原题链接在这里:https://leetcode.com/problems/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.

The successor of a node p is the node with the smallest key greater than p.val.

You will have direct access to the node but not to the root of the tree. Each node will have a reference to its parent node.

Example 1:

Input:
root = {"$id":"1","left":{"$id":"2","left":null,"parent":{"$ref":"1"},"right":null,"val":1},"parent":null,"right":{"$id":"3","left":null,"parent":{"$ref":"1"},"right":null,"val":3},"val":2}
p = 1
Output: 2
Explanation: 1's in-order successor node is 2. Note that both p and the return value is of Node type.

Example 2:

Input:
root = {"$id":"1","left":{"$id":"2","left":{"$id":"3","left":{"$id":"4","left":null,"parent":{"$ref":"3"},"right":null,"val":1},"parent":{"$ref":"2"},"right":null,"val":2},"parent":{"$ref":"1"},"right":{"$id":"5","left":null,"parent":{"$ref":"2"},"right":null,"val":4},"val":3},"parent":null,"right":{"$id":"6","left":null,"parent":{"$ref":"1"},"right":null,"val":6},"val":5}
p = 6
Output: null
Explanation: There is no in-order successor of the current node, so the answer is null.

Example 3:

Input:
root = {"$id":"1","left":{"$id":"2","left":{"$id":"3","left":{"$id":"4","left":null,"parent":{"$ref":"3"},"right":null,"val":2},"parent":{"$ref":"2"},"right":{"$id":"5","left":null,"parent":{"$ref":"3"},"right":null,"val":4},"val":3},"parent":{"$ref":"1"},"right":{"$id":"6","left":null,"parent":{"$ref":"2"},"right":{"$id":"7","left":{"$id":"8","left":null,"parent":{"$ref":"7"},"right":null,"val":9},"parent":{"$ref":"6"},"right":null,"val":13},"val":7},"val":6},"parent":null,"right":{"$id":"9","left":{"$id":"10","left":null,"parent":{"$ref":"9"},"right":null,"val":17},"parent":{"$ref":"1"},"right":{"$id":"11","left":null,"parent":{"$ref":"9"},"right":null,"val":20},"val":18},"val":15}
p = 15
Output: 17

Example 4:

Input:
root = {"$id":"1","left":{"$id":"2","left":{"$id":"3","left":{"$id":"4","left":null,"parent":{"$ref":"3"},"right":null,"val":2},"parent":{"$ref":"2"},"right":{"$id":"5","left":null,"parent":{"$ref":"3"},"right":null,"val":4},"val":3},"parent":{"$ref":"1"},"right":{"$id":"6","left":null,"parent":{"$ref":"2"},"right":{"$id":"7","left":{"$id":"8","left":null,"parent":{"$ref":"7"},"right":null,"val":9},"parent":{"$ref":"6"},"right":null,"val":13},"val":7},"val":6},"parent":null,"right":{"$id":"9","left":{"$id":"10","left":null,"parent":{"$ref":"9"},"right":null,"val":17},"parent":{"$ref":"1"},"right":{"$id":"11","left":null,"parent":{"$ref":"9"},"right":null,"val":20},"val":18},"val":15}
p = 13
Output: 15

Note:

  1. If the given node has no in-order successor in the tree, return null.
  2. It's guaranteed that the values of the tree are unique.
  3. Remember that we are using the Node type instead of TreeNode type so their string representation are different.

Follow up:

Could you solve it without looking up any of the node's values?

题解:

Successor could exist in 2 possible positions.

If x has right child, successor must be below its right child, x.right, then keep going down left.

Otherwise, successor could be x going up untill hitting first ancestor through left edge. There may be case that it keeps going up through right edge, then there is no successor.

Time Complexity: O(h). h is the height of tree.

Space: O(1).

AC Java:

 /*
// Definition for a Node.
class Node {
public int val;
public Node left;
public Node right;
public Node parent;
};
*/
class Solution {
public Node inorderSuccessor(Node x) {
if(x == null){
return x;
} if(x.right != null){
Node suc = x.right;
while(suc.left != null){
suc = suc.left;
} return suc;
} while(x.parent != null && x.parent.right == x){
x = x.parent;
} return x.parent;
}
}

LeetCode 510. Inorder Successor in BST II的更多相关文章

  1. [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 ...

  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. 本题 ...

  4. [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 ...

  5. LeetCode Inorder Successor in BST

    原题链接在这里:https://leetcode.com/problems/inorder-successor-in-bst/ Given a binary search tree and a nod ...

  6. [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 ...

  7. 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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. Cassandra数据库Java訪问

    针对的时Cassandra 2.0 数据库 Java本地client訪问Cassandra,首先建立Javaproject,使用Maven进行管理. 引入依赖: <dependency> ...

  2. smarty模板 变量 运算符 表达式 流程控制 函数

    ① 从配置文件中读取配置: 1,在模板页面加载配置文件 html页面 不是php页面<{config_load file='fo.conf'}> 2,在需要用到配置的地方加<{#si ...

  3. 爬虫入门【1】urllib.request库用法简介

    urlopen方法 打开指定的URL urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, ca ...

  4. Hibernate Criteria 查询使用

    转载 http://blog.csdn.net/woshisap/article/details/6747466 Hibernate 设计了 CriteriaSpecification 作为 Crit ...

  5. twig 截取字符串

    <p>{{content|slice(0,100)}}</p> slice()截取content变量值,从0到100

  6. activiti基础--3-----------------------------流程实例

    一.流程实例用到的表: select * from act_ru_execution #正在执行的任务表 select * from act_hi_procinst #流程实例的历史表 select ...

  7. Redis持久化——问题定位与优化(三)

    核心知识点: 1.fork操作 a.在RDB或AOF重写时,会执行fork操作创建子进程,fork操作是一个重量级操作. b.改善fork操作耗时的手段:避免使用Xen.配置Redis实例最大使用内存 ...

  8. Data Structure Array: Maximum circular subarray sum

    http://www.geeksforgeeks.org/maximum-contiguous-circular-sum/ #include <iostream> #include < ...

  9. Python 3 面向对象 一

    Python 3 面向对象 一.面向过程-->面向对象 面向过程:根据业务逻辑从上到下堆叠代码,即先干什么再干什么,基于面向过程去设计程序就好比在设计一条流水线,是一种机械式的思维方式 函数式: ...

  10. python 3 json 序列化

    python 3 json 序列化 我们学习过用eval内置方法可以将一个字符串转成python对象,不过,eval方法是有局限性的,对于普通的数据类型,json.loads和eval都能用,但遇到特 ...