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.

Example 1:

Input: root = [2,1,3], p = 1

  2
/ \
1 3

Output: 2

Example 2:

Input: root = [5,3,6,2,4,null,null,1], p = 6

      5
/ \
3 6
/ \
2 4
/
1

Output: null 这个题目思路可以用recursive方式, 去将tree换为sorted list, 然后找到p的下一个元素即可. T: O(n) S: O(n)
但是我们可以用T: O(h) S: O(1) iterable的方式, 类似于去找p, 然后给p的下一个元素即可. code
1) S: O(n)
class Solution(object):
def inorderSuccessor(self, root, p):
"""
:type root: TreeNode
:type p: TreeNode
:rtype: TreeNode
"""
stack, pre = [], None
while stack or root:
if root:
stack.append(root)
root = root.left
else:
node = stack.pop()
if pre and pre == p:
return node
pre = node
root = node.right

2) S: O(1)
class Solution:
def inorderSuccessor(self, root, p):
ans = None
while root:
if root.val > p.val:
ans = root
root = root.left
else:
root = root.right
return ans
												

[LeetCode] 285. Inorder Successor in BST_Medium tag: Inorder Traversal的更多相关文章

  1. [LeetCode] 230. Kth Smallest Element in a BST_Medium tag: Inorder Traversal

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

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

  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. [LeetCode] 106. Construct Binary Tree from Postorder and Inorder Traversal_Medium tag: Tree Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  9. LeetCode 510. Inorder Successor in BST II

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

随机推荐

  1. Esper学习之二:事件类型

    Esper对事件有特殊的数据结构约定.能处理的事件结构有:POJO,java.util.Map,Object Array,XML 1.POJO 对于POJO,Esper要求对每一个私有属性要有gett ...

  2. 关于android性能,内存优化

    转:http://www.starming.com/index.php?action=plugin&v=wave&tpl=union&ac=viewgrouppost& ...

  3. Android 使用tomcat搭建HTTP文件下载服务器

    上一篇: Android 本地搭建Tomcat服务器供真机测试 1.假设需要下载的文件目录是D:\download1(注意这里写了个1,跟后面的名称区分) 2.设置 tomcat 的虚拟目录.在 {t ...

  4. sencha touch 模仿tabpanel导航栏TabBar(2013-11-7)

    基于sencha touch 2.2所写 代码: /* *模仿tabpanel导航栏 */ Ext.define('ux.TabBar', { alternateClassName: 'tabBar' ...

  5. [转]mii-tool与ethtool的用法详解

    1.mii-tool 配置网络设备协商方式的工具: 感谢原文作者!原文地址:http://blog.chinaunix.net/uid-20639775-id-154546.html 1.1 mii- ...

  6. CF 455A Boredom

    A. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  7. DragonBones龙骨换装(局部换装+全局换装)

    参考: Egret官方换装动画 Egret换装三种方式 CSDN(全局换装) egret使用DragonBones实现简单的换装 换装,主要是替换任意插槽的图片,来达到局部换装的目的. 游戏中可以只制 ...

  8. How are you vs How are you doing

    How are you与How are you doing,有何不同呢? 貌似没有不同…… 中国教科书式的回答是"Fine, thank you, and you?" 随便一点&q ...

  9. jquery实践

    转:http://www.cnblogs.com/Wayou/p/jquery_best_prictise.html jQuery编程的最佳实践   好像是feedly订阅里看到的文章,读完后觉得非常 ...

  10. /usr/bin/ld: i386:x86-64 architecture of input file `command.o' is incompatible with i386 output

    /usr/bin/ld: i386:x86-64 architecture of input file `command.o' is incompatible with i386 output 出现这 ...