leetcode 173. Binary Search Tree Iterator
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.
设计实现一个带有下列属性的二叉查找树的迭代器:
- 元素按照递增的顺序被访问(比如中序遍历)
next()
和hasNext()
的询问操作要求均摊时间复杂度是O(1)
用stack记录从根节点道当前节点的路径,初始化的时候要找到最左边的点,也就是中序遍历的第一个点,
为了记录这个过程,把从根节点道最左下方的节点之间的节点都压栈。
next返回的是stack栈定的元素,弹出最上面的元素后,还要看一下这个被返回的元素是否有右节点,
如果有,就把右节点及所有的左侧子节点都压入栈中。
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/ public class BSTIterator { Stack<TreeNode> stack;
public BSTIterator(TreeNode root) {
stack = new Stack<TreeNode>();
while (root != null) {
stack.push(root);
root = root.left;
}
} /** @return whether we have a next smallest number */
public boolean hasNext() {
return !stack.isEmpty();
} /** @return the next smallest number */
public int next() {
TreeNode node = stack.pop();
int result = node.val;
if (node.right != null) {
node = node.right;
while(node != null){
stack.push(node);
node = node.left;
}
}
return result;
}
} /**
* Your BSTIterator will be called like this:
* BSTIterator i = new BSTIterator(root);
* while (i.hasNext()) v[f()] = i.next();
*/
leetcode 173. Binary Search Tree Iterator的更多相关文章
- leetcode@ [173] Binary Search Tree Iterator (InOrder traversal)
https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a binary searc ...
- ✡ leetcode 173. Binary Search Tree Iterator 设计迭代器(搜索树)--------- java
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- Java for LeetCode 173 Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- [leetcode]173. Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- 二叉树前序、中序、后序非递归遍历 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】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 ...
- 173. Binary Search Tree Iterator
题目: Implement an iterator over a binary search tree (BST). Your iterator will be initialized with th ...
- 【leetcode】Binary Search Tree Iterator(middle)
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
随机推荐
- js 取得 Unix时间戳(Unix timestamp)
js 取得 Unix时间戳 Unix时间戳(Unix timestamp),或称Unix时间(Unix time).POSIX时间(POSIX time),是一种时间表示方式,定义为从格林威治时间19 ...
- BZOJ 1034 题解
1034: [ZJOI2008]泡泡堂BNB Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2613 Solved: 1334[Submit][St ...
- SAP_20140304
1. SAP 主打产品 R/3 :分布式 客户端/服务器 环境的标准ERP软件. 2. 主要功能模块:销售和分销,物料管理,生产计划,质量管理,工厂维修,人力资源,工业方案,办公室和通信,项目系统 ...
- C++ stringstream介绍,使用方法与例子
From: http://www.usidcbbs.com/read-htm-tid-1898.html C++引入了ostringstream.istringstream.stringstream这 ...
- 采用管道处理HTTP请求
采用管道处理HTTP请求 之所以称ASP.NET Core是一个Web开发平台,源于它具有一个极具扩展性的请求处理管道,我们可以通过这个管道的定制来满足各种场景下的HTTP处理需求.ASP. NET ...
- 如何部署 Calico 网络?- 每天5分钟玩转 Docker 容器技术(67)
Calico 是一个纯三层的虚拟网络方案,Calico 为每个容器分配一个 IP,每个 host 都是 router,把不同 host 的容器连接起来.与 VxLAN 不同的是,Calico 不对数据 ...
- JDK配置环境变量不成功的原因
根据自己配置环境变量遇到的问题进行总结: 1.二次安装需要注意的问题 由于已经安装了一次的原因,此时的注册表已经有了安装记录. 建议删除jdk的记录 首先打开注册表 开始菜单→运行.或者直接键盘按下W ...
- JavaScript 排序算法
排序也是在程序中经常用到的算法.无论使用冒泡排序还是快速排序,排序的核心是比较两个元素的大小.如果是数字,我们可以直接比较,但如果是字符串或者两个对象呢?直接比较数学上的大小是没有意义的,因此,比较的 ...
- SQL Server 2008以上误操作数据库恢复方法——日志尾部备份
原文出处:http://blog.csdn.net/dba_huangzj/article/details/8491327 问题: 经常看到有人误删数据,或者误操作,特别是update和delete的 ...
- redis、memcache、mongoDB 对比
从以下几个维度,对 redis.memcache.mongoDB 做了对比. 1.性能 都比较高,性能对我们来说应该都不是瓶颈. 总体来讲,TPS 方面 redis 和 memcache 差不多,要大 ...