173 Binary Search Tree Iterator 二叉搜索树迭代器
实现一个二叉搜索树迭代器。你将使用二叉搜索树的根节点初始化迭代器。
调用 next() 将返回二叉搜索树中的下一个最小的数。
注意: next() 和hasNext() 操作的时间复杂度是O(1),并使用 O(h) 内存,其中 h 是树的高度。
详见:https://leetcode.com/problems/binary-search-tree-iterator/description/
Java实现:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class BSTIterator { private Stack<TreeNode> stk=new Stack<TreeNode>();
public BSTIterator(TreeNode root) {
while(root!=null){
stk.push(root);
root=root.left;
}
} /** @return the next smallest number */
public int next() {
TreeNode node=stk.pop();
int val=node.val;
if(node.right!=null){
node=node.right;
while(node!=null){
stk.push(node);
node=node.left;
}
}
return val;
} /** @return whether we have a next smallest number */
public boolean hasNext() {
return !stk.isEmpty();
}
} /**
* Your BSTIterator object will be instantiated and called as such:
* BSTIterator obj = new BSTIterator(root);
* int param_1 = obj.next();
* boolean param_2 = obj.hasNext();
*/
参考:https://www.cnblogs.com/grandyang/p/4231455.html
173 Binary Search Tree Iterator 二叉搜索树迭代器的更多相关文章
- [leetcode]173. Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- Leetcode173. Binary Search Tree Iterator二叉搜索树迭代器
实现一个二叉搜索树迭代器.你将使用二叉搜索树的根节点初始化迭代器. 调用 next() 将返回二叉搜索树中的下一个最小的数. 注意: next() 和hasNext() 操作的时间复杂度是O(1),并 ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树
4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [LeetCode] Validate Binary Search Tree 验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- LeetCode 235. Lowest Common Ancestor of a Binary Search Tree (二叉搜索树最近的共同祖先)
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- LeetCode 669. Trim a Binary Search Tree修剪二叉搜索树 (C++)
题目: Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so th ...
随机推荐
- Visual Studio VS如何卸载Visual assistant
1 点击工具-扩展管理器 2 选中Visual Assist X,点击卸载即可.
- 解决UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 12: ordinal not in range(128)的编码问题
当我在运行一个基于scrapy的爬虫时出现UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 12: ordina ...
- MYSQL 增加字段不报错,插入数据不报错处理
') ON DUPLICATE KEY UPDATE sort_name = "vipset"; 重点在 ON DUPLICATE KEY UPDATE sort_name = & ...
- url优化|隐藏index.php
隐藏index.php 一.codeigniter codeigniter和许多php框架一样,有个单一入口index.php,从url上看,显得很不友好.通过apache的rewirte,是可以 ...
- LoadRunner系列之—-02 基于webservice协议的接口测试(脚本实例)
Loadrunner 基于webservice协议的接口压力测试(脚本实例) 接口功能如下:请求接口,报文只有一个参数为证件号码:返回报文中,有证件号码是否能查到对应数据,查到几条数据. 思路:请求w ...
- 《modern operating system》 chapter 6 DEADLOCKS 笔记
DEADLOCKS Both processes are blocked and will remain so forever. This situation is called a deadlock ...
- sql server 系统常用函数:聚合函数 数学函数 字符串函数 日期和时间函数和自定义函数
一.系统函数 1.聚合函数 聚合函数常用于GROUP BY子句,在SQL Server 2008提供的所有聚合函数中,除了COUNT函数以外,聚合函数都会忽略空值AVG.COUNT.COUNT_BIG ...
- 5 微信票据 access_token--开发微信的第二道坎儿
一 access_token基本概念 定义:access_token是公众号的全局唯一接口调用凭据,公众号调用各接口时都需使用access_token.开发者需要进行妥善保存. 时效性:access_ ...
- JavaScript基础教程复习笔记
document.write("<h1>这是一个标题</h1>"); 您只能在 HTML 输出中使用 document.write.如果您在文档加载后使用该 ...
- ubuntu截图工具及GNOME的使用及类似qq截图快捷键
1:了解gnome参数 gnome-screenshot -h