[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 ...
随机推荐
- sencha touch carousel 扩展 CardList 可绑定data/store
扩展代码: /* *扩展carousel *通过data,tpl,store配置数据 */ Ext.define('ux.CardList', { extend: 'Ext.carousel.Caro ...
- mysqlslap 压测工具使用说明
PS:今天一同事问我有木有比较靠谱的mysql压力测试工具可用.其实mysql自带就有一个叫mysqlslap的压力测试工具,还是模拟的不错的.下面举例说说.mysqlslap是从5.1.4版开始的一 ...
- ssh命令远程登录
1.查看SSH客户端版本 有的时候需要确认一下SSH客户端及其相应的版本号.使用ssh -V命令可以得到版本号.需要注意的是,Linux一般自带的是OpenSSH: 下面的例子即表明该系统正在使用Op ...
- 实战BRTSvc一款我见过的最嚣张的挖矿软件
第一步:发现告警 Suricata发现特征字符串jsonrpc,这个是匹配挖矿木马的一个重要特征.于是开始分析告警信息: 告警中可以提取出的有效信息如下: 目标IP:149.28.199.108 目标 ...
- iOS - 沙盒机制(SandBox)和获取沙盒路径
iOSAPP可以在自己的沙盒里读写文件,但是,不可以访问其他APP的沙盒.每一个APP都是一个信息孤岛,相互是不可以进行通信的,唯独可以通过URLScheme.沙盒里面的文件可以是照片.声音文件.文本 ...
- iOS ViewControllers 瘦身
https://objccn.io/issue-1-1/ https://juejin.im/user/57ddfba4128fe10064cbb93a 把 Data Source 和其他 Proto ...
- HOJ 2133&POJ 2964 Tourist(动态规划)
Tourist Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1503 Accepted: 617 Description A ...
- 2.5BatchNormalzation
BatchNormalzation是一种解决深度神经网络层数太多,而没有办法有效前向传递的问题,因为每层的输出值都会有不同的均值和方差,所以输出数据的分布也不一样. 如果对于输入的X*W本身得到的值通 ...
- 基于Docker部署nodejs应用
基于Docker部署nodejs应用 背景 公司基于Vue.js的项目最近需要部署到云端,因此需要先行在公司内部Docker环境下验证相关技术,因而有本文之前提. 本文展示在Docker容器中,应用部 ...
- CCCC 月饼
https://www.patest.cn/contests/gplt/L2-003 题解:按平均值贪心. 坑:有一个样例卡住了,是因为 while (i<=n&&x - bs[ ...