[LeetCode] 285. Inorder Successor in BST_Medium tag: Inorder Traversal
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的更多相关文章
- [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 ...
- [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 ...
- 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. 本题 ...
- [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 ...
- LeetCode Inorder Successor in BST
原题链接在这里:https://leetcode.com/problems/inorder-successor-in-bst/ Given a binary search tree and a nod ...
- 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 ...
- [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 ...
- [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 ...
- LeetCode 510. Inorder Successor in BST II
原题链接在这里:https://leetcode.com/problems/inorder-successor-in-bst-ii/ 题目: Given a binary search tree an ...
随机推荐
- Delphi Code Editor 之 基本操作
Delphi Code Editor 之 基本操作 毫无疑问,Delphi是高度可视化的.这是使用Delphi进行编程的最大好处之一.当然,任何一个有用的程序中都有大量手工编写的代码.当读者开始编写应 ...
- Android应用的自动升级、更新模块的实现(转)
我们看到很多Android应用都具有自动更新功能,用户一键就可以完成软件的升级更新.得益于Android系统的软件包管理和安装机制,这一功能实现起来相当简单,下面我们就来实践一下.首先给出界面效果: ...
- springbatch---->springbatch的使用(五)
这里我们介绍一个从数据库读取数据并写入到文件中的案例.如果能真心爱上一个人,那么不管对方是何等恶劣,哪怕对方并不爱自己,人生也至少不会是地狱,就算多少有点黯淡. 读取数据库数据 一.定义一个读写的jo ...
- canvas练习 - 七巧板绘制
用到的方法: 注意点: stokeStyle等样式要在stroke前边 如果最后只有一个stroke或者fill,那么只填充最后一次路径的,之前的也会画出来但是没有填充看不到.所以每次begin+cl ...
- 关于SQL优化(转载,格式有调整)
一.问题的提出 在应用系统开发初期,由于开发数据库数据比较少,对于查询SQL语句,复杂视图的的编写等体会不出SQL语句各种写法的性能优劣,但是如果将应用 系统提交实际应用后,随着数据库中数据的增加,系 ...
- 使用SQLite3工具查看sqlite.db文件
http://www.sqlite.org OS X自从10.4后把SQLite这套相当出名的数据库软件,放进了作业系统工具集里.OS X包装的是第三版的SQLite,又称SQLite3.这套软件有几 ...
- LeetCode 77 Combinations(排列组合)
题目链接:https://leetcode.com/problems/combinations/#/description Problem:给两个正数分别为n和k,求出从1,2.......n这 ...
- sencha touch 类的使用
sencha touch 有一套自己的类机制,可以以面向对象的方式去写代码,封装业务逻辑,sencha touch 的组件.插件.api等都建立在这一套类机制的上面 在实际开发中,我们需要遵循这一套机 ...
- JavaScript 异步进化史
前言 JS 中最基础的异步调用方式是 callback,它将回调函数 callback 传给异步 API,由浏览器或 Node 在异步完成后,通知 JS 引擎调用 callback.对于简单的异步操作 ...
- 基本类型算法题目学习(EPI)
1.关于奇偶校验的方法中,如何快速的求取一个64-bit的数字的奇偶校验位.(如果1的位数为奇数,则奇偶校验位为1,如果1的位数为偶数,则奇偶校验位为0) a.暴力枚举法采用一位一位进行计算,一位一位 ...