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. 本版本延续MVC中的统一验证机制~续的这篇文章,本篇主要是对验证基类的扩展和改善(转)

    本版本延续MVC中的统一验证机制~续的这篇文章,本篇主要是对验证基类的扩展和改善 namespace Web.Mvc.Extensions { #region 验证基类 /// <summary ...

  2. 一个linux命令之grep---1

    grep   表示“匹配” 参数常用的四个 -i    匹配的文件内容忽略大小写 -R   递归的匹配文件(即在一层一层的目录中的文件中去匹配) -n   表示匹配出的行显示在文件中的行号 -H   ...

  3. Jrebel不生效的原因和解决办法

    一.问题原因和解决办法 我这里用的是idea,装了jrebel.之前用的好好的. 后边新建了一个project,不知道为啥,感觉总是不生效,虽然显示class reload了,但感觉还是没起作用. 后 ...

  4. 【咸鱼教程】BitmapLabel位图字体使用

    引擎版本3.2.6 教程目录一 为什么要使用位图字体二 如何使用位图字体2.1 TextureMerger制作位图字体2.2 exml中使用位图字体三 Demo源码 一  为什么要使用位图字体egre ...

  5. was cached in the local repository, resolution will not be reattempted until the update interval of localhost-repository has elapsed or updates are forced

    ailed to collect dependencies at com.eshore:common:jar:0.0.1-SNAPSHOT: Failed to read artifact descr ...

  6. python---修改编辑器的配色和字体大小

    因为习惯黑色的背景,所以必须修改成对应的配色: 在这里设置theme: 设置字体大小: 找到Font,这里设置字体大小,首先要Scheme 后 进行 Save as 操作后,才能设置 Size ,设置 ...

  7. wpgcms---详情页面数据怎么渲染

    wpgcms的详情页面的数据会被保存在 contentInfo 这么一个字段里面. 面包屑导航调用: <p>当前位置 {% for c in crumb|slice(1, crumb|le ...

  8. 9.8Django

    2018-9-8 14:34:38

  9. Google Drive 里的文件下载的方法

    Google Drive 里并不提供创建直接下载链接的选项,但是可以通过小小的更改链接形式就能把分享的内容保存到本地.例如,一份通过 Google Drive 分享的文件链接形式为: https:// ...

  10. 一些常用的opencv函数

    分配图像空间: IplImage* cvCreateImage(CvSize size, int depth, int channels);       size:  cvSize(width,hei ...