LeetCode Binary Search Tree Iterator
原题链接在这里:https://leetcode.com/problems/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.
题解:
用一个stack把所有最左边的node压进 stack中。
判断hasNext()时就是看stack 是否为空.
next()返回stack顶部top元素,若是top有右子树,就把右子树的所有最左node压入stack中.
constructor time complexity: O(h).
hashNext time complexity: O(1).
next time complexity: O(h).
Space: O(h), stack 最大为树的高度。
AC Java:
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/ public class BSTIterator {
Stack<TreeNode> stk; public BSTIterator(TreeNode root) {
stk = new Stack<TreeNode>();
while(root != null){
stk.push(root);
root = root.left;
}
} /** @return whether we have a next smallest number */
public boolean hasNext() {
return !stk.isEmpty();
} /** @return the next smallest number */
public int next() {
TreeNode top = stk.pop();
TreeNode rightLeft = top.right;
while(rightLeft != null){
stk.push(rightLeft);
rightLeft = rightLeft.left;
} return top.val;
}
} /**
* Your BSTIterator will be called like this:
* BSTIterator i = new BSTIterator(root);
* while (i.hasNext()) v[f()] = i.next();
*/
类似Binary Tree Inorder Traversal, Inorder Successor in BST.
LeetCode Binary Search Tree Iterator的更多相关文章
- LeetCode: Binary Search Tree Iterator 解题报告
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
- [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
Description: Implement an iterator over a binary search tree (BST). Your iterator will be initialize ...
- [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 python
# Definition for a binary tree node # class TreeNode(object): # def __init__(self, x): # self.val = ...
- 【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-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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
随机推荐
- MySQL 用户管理——权限表
权限表 权限表存放在mysql数据库中 user表结构 用户列:Host.User.Password 权限列:*priv 资源控制列:max* 安全列:其余 db表 存储了用户对某个数据库的操作权 ...
- OpenCV2.4.10 Mac Qt Configuration
Download OpenCV 2.4.10 Download CMake 2.8 Open CMake and choose the source code directory and build ...
- Scala:使用Sublime开发Scala
Scala:使用Sublime开发Scala 第一步:[Tools][Build System][New Build System] 第二步:在打开的新文件中输入: { //"cmd&quo ...
- WPF 傻瓜生成 .dbml文件,以及文件用途原理是什么
- [转]一步一步asp.net_购物车订单与支付宝
本文转自:http://www.cnblogs.com/mysweet/archive/2012/05/19/2508534.html 最近这几天很忙,一边忙着准备一堆课程设计(8门专业课.....伤 ...
- Mongodb 创建索引
db.getCollection('ct_project').ensureIndex({'pro_code':1}) 创建索引 db.getCollection('ct_project').ensu ...
- NOJ 1641 错误的算法(模拟)
[1641] 错误的算法 时间限制: 5000 ms 内存限制: 65535 K 问题描述 有道题目是这样的: 输入一个 n 行 m 列网格,找一个格子,使得它所在的行和列中所有格子的数之和最大.如果 ...
- string[1]:size 属性具有无效大小值0
截图 使用存储过程返回多个字符串参数 程序 public class EventStatisticsDAL { public static void GetCount(string code, out ...
- Tortoise SVN 不显示 Log Message 具体信息的解决方法
今天加入新项目,在 Tortoise SVN Check out 完项目之后,发现右键 show log 不显示 Log Message 的具体信息: 因为是新加入的项目,问了原来负责这个项目的同事, ...
- RadioGroup和ViewPager实现Tab
Activity的布局文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ...