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 ...
随机推荐
- Java 实现 SSH 协议的客户端登录认证方式--转载
背景 在开篇之前,让我们先对 SSH 协议有个宏观的大致了解,这样更有利于我们对本文的加深了解.首先要提到的就是计算机网络协议,所谓计算机网络协议,简单的说就是定义了一套标准和规则,使得不同计算机之间 ...
- navigaitonBar的自定义设置
navigaitonBar的自定义设置 navigationBar介绍: navigationbar就是一个导航视图控制器上面的导航栏. 如何设置这个navigationbar? 首先我们来探讨如何来 ...
- [转] Javascript中数组与字典(即object)的使用
简述: 简单记录一下数据结构字典和数组, 其实在Javascript这种弱类型的脚本语言中,数组同时也就是字典,下面主要就是字典数组的简易使用 代码: 1. 数组中添加map <!DOCTYPE ...
- TCP/IP协议原理与应用笔记09:数据通信---封装
2016-08-091. 数据通信----封装: 2. 协议数据单元: PDU:对等层数据通信的单元. 比如Source端的应用层 和 Destination端的应用层是对等层(L7),这个时候L7 ...
- Web.Config文件中添加数据库配置文件
1获取所有配置文件节点的类ConfigurationManager 2数据库节点<ConnectionStrings> <add> name ="Sqlconnect ...
- Android-adb相关
最近做android开发遇到无法通过usb链接设备的情况,通过wifi连接设备调试也颇为方便 1.android 要root , 下载终端app 比如 BetterTerminal 2.通过以下命令 ...
- sql问题
表中某个指标重复,去掉重复项: select * from #temp where A0107 in (select A0107 from #temp group by A0107having CO ...
- 使用<pre>标签为你的网页加入大段代码
在上节中介绍加入一行代码的标签为<code>,但是在大多数情况下是需要加入大段代码的,如下图: 怎么办?不会是每一代码都加入一个<code>标签吧,没有这么复杂,这时候就可以使 ...
- Spring 环境搭建
1.导包 2.编写Helloworld程序 package cn.test.helloWorld; public class HelloWorld { public void sayHello(){ ...
- css expression explaination
http://blogs.msdn.com/b/ie/archive/2008/10/16/ending-expressions.aspx 据说已经被弃用的IE css写法,为了修复一些IE8及老版本 ...