题目:

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. 几种常见的排序方法(C语言实现)

    #include <stdio.h> #include <stdlib.h> #include <Windows.h> //直接插入排序 void InsertSo ...

  2. String、StringBuilder、StringBuffer

    String                                                                                        String ...

  3. android listview和button,ImageButton等有事件的控件的总结

    public ImageButton(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defSty ...

  4. 也发一个自己实现的android简单文件选择器代码。支持多卡,排序

    一个很简单的文件选择器对话框,支持双sd卡,当然前提是要有sd卡..并且实现了排序效果. 只有100多行的代码,基本的思路就是用listview显示目录下的所有子文件,再判断是文件还是目录. 利用Co ...

  5. Netsharp快速入门(之5) 基础档案(之D 实体建模 生成实体代码、同步数据库、配置插件运行时)

    作者:秋时 杨昶   时间:2014-02-15  转载须说明出处 3.3.1  同步数据库并生成dll文件 1.在基础档案和销售管理项目上右击,选择同步数据库结构来创建数据库表 2. 在基础档案项目 ...

  6. PHP图形图像处理之初识GD库

    d=====( ̄▽ ̄*)b 引语 php不仅仅局限于html的输出,还可以创建和操作各种各样的图像文件,如GIF.PNG.JPEG.WBMP.XBM等. php还可以将图像流直接显示在浏览器中. 要处 ...

  7. IE6中布局常见问题

    1.众所周知,每个IE的版本都有两种模式,怪异模式(混杂模式)和标准模式.下图附上针对IE的hack. 2.另外有一种引进css的方法,也可以作为调整网站hack的方法:<!—[if IE 6] ...

  8. cf 496B Secret Combination

    题目链接:B. Secret Combination You got a box with a combination lock. The lock has a display showing n d ...

  9. 先进的自动布局工具箱(autolayout)

    原文:Advanced Auto Layout Toolbox 这篇文章并没有具体介绍自动布局的一些基本概念,主要讲解了一些高级的使用方法和调试技巧,文中有的句子比较长,意思也有点难懂,所以需要静下心 ...

  10. PHP 扩展库

    表 6.1. PHP 扩展库 扩展库 说明 注解 php_bz2.dll bzip2 压缩函数库 无 php_calendar.dll 历法转换函数库 自 PHP 4.0.3 起内置 php_cpdf ...