Binary Search Tree Iterator——LeetCode
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.
Calling next()
will return the next smallest number in the BST.
Note: next()
and hasNext()
should run in average O(1) time and uses O(h) memory, where h is the height of the tree.
题目大意:基于一个二叉搜索树实现一个iterator,调用next()返回BST中下一个最小的数。
解题思路:我是在生成这个iterator的时候,预处理这棵树,以后所有的操作都是O(1)。
public class BSTIterator { private Stack<TreeNode> stack = new Stack<>(); public BSTIterator(TreeNode root) {
init(root);
} private void init(TreeNode node){
if(node==null){
return;
}
init(node.right);
stack.push(node);
init(node.left);
} /** @return whether we have a next smallest number */
public boolean hasNext() {
return !stack.isEmpty();
} /** @return the next smallest number */
public int next() {
return stack.pop().val;
}
Binary Search Tree Iterator——LeetCode的更多相关文章
- Binary Search Tree Iterator leetcode
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- 【leetcode】Binary Search Tree Iterator
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
- 【LeetCode】173. Binary Search Tree Iterator (2 solutions)
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
- LeetCode: Binary Search Tree Iterator 解题报告
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
- leetcode-173:Binary Search Tree Iterator(Java)
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
- 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator
144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- LeetCode Binary Search Tree Iterator
原题链接在这里:https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a bina ...
- leetcode@ [173] Binary Search Tree Iterator (InOrder traversal)
https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a binary searc ...
随机推荐
- iOS报错Expected selector for Objective-C method
这个报错非常恶心:原因竟然是在导入头文件的地方多写了一个"+"号,可能问题在一个文件,报错在另一个文件
- (转)一步一步学习PHP(3)——函数
相信每个人在学习PHP之前至少都有着一定的C语言,或者是C++/Java/C#等其他语言的基础,所以在这里也不从头开始说起,只是来谈谈PHP方法的独特之处. 1. 方法概述 首先,写一个最简单的函数, ...
- leetcode修炼之路——13. Roman to Integer
题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...
- C#基础知识01(continue、break 和 return、ref 和 out)
break[跳出循环或者退出一个switch语句]由于它是用来退出循环或者switch语句的,所以只有当它出现在这些语句中时才是合法的. continue 语句和break语句相似,只是它不是退出一个 ...
- 如何修改UITableView每个cell的分隔线和左边的距离?
在ios7中,UITableViewCell左侧会有默认15像素的空白.这时候,设置setSeparatorInset:UIEdgeInsetsZero 能将空白去掉.但是在ios8中,设置setSe ...
- Active控件有关问题
ActiveX 控件是允许网站提供视频等内容的网站. 当你浏览 Web 时,它们允许你使用工具栏.股票代号.视频和其它内容. 但是,这些程序有时可能出现问题,或者向你提供不需要的内容. 在某些情况下, ...
- git config --global core.excludesfile配置gitignore全局文件
Linux中,这时把全局要忽略的文件列表.gitignore放当前用户根目录下: git config --global core.excludesfile '~/.gitignogtire' Win ...
- 由于权限不足而无法读取配置文件出现的HTTP 500.19解决办法
无法访问请求的页面,因为该页的相关配置数据无效. 如下图: 解决方法, 到站点目录的属性,安全标签,添加用户(Everyone),并给修改权限:
- .net发邮件
// 引入命名空间 using System.Net; using System.Net.Mail; SmtpClient smtp = new SmtpClient(); //实例化一个SmtpCl ...
- PIL Image 转成 wx.Image、wx.Bitmap
import wx from PIL import Image def ConvertToWxImage(): pilImage = Image.open('1.png') image = wx.Em ...