题目:

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. visualgo 数据结构与算法可视化工具

    推荐可视化数据结构与算法工具 http://zh.visualgo.net/

  2. 自定义右键菜单,禁用浏览器自带的右键菜单[右键菜单实现--Demo]

    许多从事Web开发的会发现有些事,我们需要禁用浏览器本事自带的右键菜单,而实现自定义的右键菜单下面我们也来实现一个自定义的右键菜单 首先来创建JSP页面 <%@ page language=&q ...

  3. 用setTimeout 代替 setInterval实时拉取数据

    在开发中,我们常常碰到需要定时拉取网站数据,如: setInterval(function(){ $.ajax({ url: 'xx', success: function( response ){ ...

  4. 机器学习&&数据挖掘之一:决策树基础认识

    决策树入门篇 前言:分类是数据挖掘中的主要分析手段,其任务就是对数据集进行学习并构造一个拥有预测功能的分类模型,用于预测未知样本的类标号,把类标号未知的样本按照某一规则映射到预先给定的类标号中. 分类 ...

  5. 【BZOJ】【2756】【SCOI2012】奇怪的游戏

    网络流-最大流 这题……建模部分先略过 这道题是会卡时限的T_T俺的Dinic被卡了,在此放几篇很棒的讲网络流算法的文章,至于大家耳熟能详的论文就不放了…… http://www.cppblog.co ...

  6. SQL 基本(Head First)

    CREATE TABLE my_contacts( last_name VARCHAR(30), first_name VARCHAR(30), email VARCHAR(50), gender C ...

  7. jdbc读取数据库表

    把结果集封装为List // 通过结果集元数据封装List结果集 public static List<Map<String, Object>> read(String sql ...

  8. sencha Touch 2.4 学习之 XTemplate模板

    XTemplate模板 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> & ...

  9. web之困:现代web应用安全指南

    <web之困:现代web应用安全指南>在web安全领域有“圣经”的美誉,在世界范围内被安全工作者和web从业人员广为称道,由来自google chrome浏览器团队的世界顶级黑客.国际一流 ...

  10. SQL技术内幕-12 SQL优化方法论前言

    我推荐的一种使用自顶向下的优化论.这种方法,首先分析实例级的等待时间,在通过一系列步骤将其不断细化,知道找出系统中导致大量等待的进程/组件.一旦找出这些令人讨厌的进程,就可以集中优化他们了,一下是这种 ...