173. Binary Search Tree Iterator
来源:互联网 发布:淘宝嘉年华是什么活动 编辑:程序博客网 时间:2024/06/05 22:43
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.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
public class BSTIterator { TreeNode root; ArrayList<Integer> ls = new ArrayList<Integer>(); int point = 0; int n = 0; public BSTIterator(TreeNode root) { this.root = root; inorder(root); n = ls.size(); } private void inorder(TreeNode root) { if(root == null) return; if(root.left != null) inorder(root.left); ls.add(root.val); if(root.right != null) inorder(root.right); } /** @return whether we have a next smallest number */ public boolean hasNext() { if(point < n) return true; else return false; } /** @return the next smallest number */ public int next() { return ls.get(point++); }}
0 0
- 173. Binary Search Tree Iterator
- 173. Binary Search Tree Iterator
- 173. Binary Search Tree Iterator
- 173. Binary Search Tree Iterator
- 173. Binary Search Tree Iterator
- 173. Binary Search Tree Iterator
- 173. Binary Search Tree Iterator
- 173. Binary Search Tree Iterator
- 173. Binary Search Tree Iterator
- 173. Binary Search Tree Iterator
- 173. Binary Search Tree Iterator
- 173. Binary Search Tree Iterator
- 173. Binary Search Tree Iterator**
- 173. Binary Search Tree Iterator
- 173. Binary Search Tree Iterator
- 173. Binary Search Tree Iterator
- 173. Binary Search Tree Iterator
- 173. Binary Search Tree Iterator
- Glide源码解析05-onLoadComplete
- Glide源码解析06-Glide流程图
- TomCat的配置和使用
- PAT1060 爱丁顿数(25)
- 执行yun安装时/var/run/yum.pid 已被锁定,PID 为 *** 的另一个程序正在运行。
- 173. Binary Search Tree Iterator
- Spring注解的默认 bean 的 id
- linux ls
- matlab实用技巧
- 对JAVA静态成员的理解
- Enabling Software BCH ECC on a Linux Platform
- windows 7 调整无线和有线网络的优先级
- javaSE_8系列博客——Java语言的特性(三)--类和对象(19)--嵌套类(Lambda 表达式)
- linux cd