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. 文件系统的挂载(2)---挂载rootfs文件系统

    一.目的 本文主要讲述linux内核rootfs文件系统的挂载过程,内核版本为3.10. rootfs是基于内存的文件系统,没有实际的存储设备,所有操作都在内存中完成.为了保证linux内核的精简性, ...

  2. 【大数据系列】hadoop单节点安装官方文档翻译

    Hadoop: Setting up a Single Node Cluster. HADOOP:建立单节点集群 Purpose Prerequisites Supported Platforms R ...

  3. Android NDK学习(4)使用cygwin生成.so库文件

    转:http://www.cnblogs.com/fww330666557/archive/2012/12/14/2817389.html 简单的示例: makefile文件: LOCAL_PATH: ...

  4. 压力测试工具JMeter入门教程<转>

    1.Jmeter 概要描叙 jmeter 是一款专门用于功能测试和压力测试的轻量级测试开发平台.多数情况下是用作压力测试,该测试工具在阿里巴巴有着广泛的使用,估计是不要钱吧,哈哈,功能上来说,整个平台 ...

  5. PHP内置安全函数一览

    内置安全函数 filter_var函数 根据参数中的过滤类型进行过滤,如过滤Email类型的,则符合的字符串返回字符串,不符合的返回False. urldecode函数 写这个函数是特别为了提醒注意, ...

  6. 从浏览器输入URL到页面渲染的过程

    零.背景 一个web安全工程师在学习web安全和web渗透时候,非常有必要了解整个WEB工作过程. 一.输入URL 这里是最基本的知识:URL是URI的一种实际应用,URI统一资源表示符,URL统一资 ...

  7. C# 批量上传

    完整例子下载 效果: 前台: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="d ...

  8. python pytest测试框架介绍三

    之前介绍了pytest以xUnit形式来写用例,下面来介绍pytest特有的方式来写用例 1.pytest fixture实例1 代码如下 from __future__ import print_f ...

  9. python unittest框架中doCleanups妙用

    偶看unittest官方文档中,发现一个很好用的功能函数doCleanups,看看官方是怎么解释的: doCleanups() This method is called unconditionall ...

  10. zabbix监控tcp状态

    Tcp的连接状态对于我们web服务器来说是至关重要的,从TCP的连接状态中可以看出网络的连接情况,服务器的压力情况,对服务器的并发有很好的直观反映:尤其是并发量ESTAB:或者是syn_recv值,假 ...