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 ...
随机推荐
- 安全的PHP代码编写准则(转)
绝不要信任外部数据或输入 关于 Web 应用程序安全性,必须认识到的第一件事是不应该信任外部数据.外部数据(outside data) 包括不是由程序员在 PHP 代码中直接输入的任何数据.在采取措施 ...
- Swift的闭包(一):闭包简介、闭包表达式的优化
定义:Closures are self-contained blocks of functionality that can be passed around and used in your co ...
- Manacher算法求回文半径
http://wenku.baidu.com/link?url=WFI8QEEfzxng9jGCmWHoKn0JBuHNfhZ-tKTDMux34CeY8UNUwLVPeY5HA3TyoKU2XegX ...
- niop 2014寻找道路
/* 乍一看就是个最短路 SFPA 但是要保证路径上的所有点的出边所指向的点都直接或间接与终点连通. 这一点就蛋疼了0.0 开始想的是正着跑一边 每一个点的所有边都能符合条件 那这个点就符合条件0.0 ...
- Session深入理解
Session是在什么情况下产生的 客户端访问服务器端,服务器端为每个用户生成一个唯一的sessionId,是这样吗?sessionId的作用是什么? http://www.cnblogs.com/s ...
- ExcelApplication 另存Excel的SaveAs函数
procedure SaveAs(const Filename: WideString; FileFormat: OleVariant; Password: OleVariant; WriteResP ...
- strace 使用
- addEventListener之handleEvent
addEventListener() 方法是将指定的事件监听器注册到目标对象上,当该对象触发指定的事件时,指定的回调函数就会被执行.语法: element.addEventListener(type, ...
- uva 11673 Garbage Remembering Exam (概率)
题目链接: http://vjudge.net/problem/viewProblem.action?id=42000 该过程为随即过程,因此总期望值等于个单词对应的期望值,即它们wasted的概率 ...
- C# ,asp.net 获取当前,相对,绝对路径(转)
C# ,asp.net 获取当前,相对,绝对路径 一.C#获取当前路径的方法: . System.Diagnostics.Process.GetCurrentProcess().MainModule. ...