题目:

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.

链接: http://leetcode.com/problems/inorder-successor-in-bst/

题解:

一开始的想法就是用inorder traversal,设置一个boolean变量,当找到root.val = p.val的时候返回下一个节点,遍历完毕以后返回null。

Time Complexity - O(n), Space Complexity - O(n)

/**
* 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;
}
boolean foundNodeP = false;
Stack<TreeNode> stack = new Stack<>();
while(root != null || !stack.isEmpty()) {
if(root != null) {
stack.push(root);
root = root.left;
} else {
root = stack.pop();
if(foundNodeP) {
return root;
}
if(root.val == p.val) {
foundNodeP = true;
}
root = root.right;
}
} return null;
}
}

看了Discuss之后发现有很多简洁的写法,而且不用遍历全部元素。利用BST的性质,比较root.val和p.val,然后在左子树或者右子树中查找。

Time Complexity - O(h), Space Complexity - O(1)

/**
* 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;
}
}

二刷:

方法和一刷一样。我们先建立一个空的successor,再取得一个root节点的reference。每次当node.val > p.val的时候,我们记录下当前的node节点,然后往左子树查找。否则向右子树查找。 向右子树查找的过程中不需要更新successor。

Java:

Time Complexity - O(h), Space Complexity - O(1)

/**
* 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) {
TreeNode node = root, successor = null;
while (node != null) {
if (node.val > p.val) {
successor = node;
node = node.left;
} else {
node = node.right;
}
}
return successor;
}
}

Reference:

https://leetcode.com/discuss/69200/for-those-who-is-not-so-clear-about-inorder-successors

https://leetcode.com/discuss/61105/java-python-solution-o-h-time-and-o-1-space-iterative

https://leetcode.com/discuss/59728/10-and-4-lines-o-h-java-c

https://leetcode.com/discuss/59787/share-my-java-recursive-solution

285. Inorder Successor in BST的更多相关文章

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

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

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

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

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

随机推荐

  1. 丢掉 WinPE,使用 DISKPART 来分区吧

    自 Windows Vista 之后的操作系统,如果在安装系统的时候使用 Windows 自带的分区功能,则会多出一个 100M 的系统保留分区.这会让一个物理硬盘,原先最多可以分 4 个主分区的,现 ...

  2. EJB总结

    http://blog.sina.com.cn/s/blog_8ce4eca80101azdd.htmlEJB详解 http://blog.csdn.net/han_yankun2009/articl ...

  3. dd面试经历

     HR面:看了我的简历,说fe做的简历就是不一样哈哈好吧,然后随便问了点项目,又问了什么时候可以去实习,就没了.三面:基本数据结构.冒泡排序.数组去重.ie与主流浏览器事件绑定.垂直居中的css实现方 ...

  4. Python科学计算(二)windows下开发环境搭建(当用pip安装出现Unable to find vcvarsall.bat)

    用于科学计算Python语言真的是amazing! 方法一:直接安装集成好的软件 刚开始使用numpy.scipy这些模块的时候,图个方便直接使用了一个叫做Enthought的软件.Enthought ...

  5. UML 用例图,时序图,活动图的定义以及区别

    1.用例图,时序图,活动图的定义 1.用例图:  用例图描述了系统提供的一个功能单元.用例图的主要目的是帮助开发团队以一种可视化的方式理解系统的功能需求,包括基于基本流程的"角色" ...

  6. C++ Template之技巧性基础知识

    1.对于T是自定义类型的,如果存在子类型则需要在模版内部加上typename 示例代码: template<typename T> class Myclass { typename T:: ...

  7. OrzFAng系列–树 解题报告

    题目描述 方方方种下了三棵树,两年后,第二棵树长出了n个节点,其中1号节点是根节点. 给定一个n个点的树 支持两种操作 方方方进行m次操作,每个操作为: (1)给出两个数i,x,将第i个节点的子树中, ...

  8. Leetcode#166 Fraction to Recurring Decimal

    原题地址 计算循环小数 先把负数转化成正数,然后计算,最后添加符号 当被除数重复出现的时候,说明开始循环了,所以用一个map保存所有遇到的被除数 需要考虑溢出问题,这也是本题最恶心的地方,看看通过率吧 ...

  9. ActiveMQ主从配置

    这种方式有个问题,activemq1有消息没消费完但是突然宕机,虽然程序会自动连到activemq2.但是activemq1的消息只有等机器恢复后才会被消费. 1.启动:我这里使用的是apache-a ...

  10. [百度空间] [转]DLL地狱及其解决方案

    DLL地狱及其解决方案 原作者:Ivan S Zapreev 译者:陆其明概要 本文将要介绍DLL的向后兼容性问题,也就是著名的“DLL Hell”问题.首先我会列出自己的研究结果,其中包括其它一些研 ...