【leetcode】Binary Search Tree Iterator(middle)
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.
思路:
说白了,就是把中序遍历拆成几个部分写。
我的代码:
class BSTIterator {
public:
BSTIterator(TreeNode *root) {
pCur = root;
while(pCur != NULL)
{
v.push_back(pCur);
pCur = pCur->left;
}
} /** @return whether we have a next smallest number */
bool hasNext() {
return (!v.empty() || NULL != pCur);
} /** @return the next smallest number */
int next() {
int num;
TreeNode * tmp = v.back();
v.pop_back();
num = tmp->val;
pCur = tmp->right;
while(pCur != NULL)
{
v.push_back(pCur);
pCur = pCur->left;
}
return num;
}
private:
vector<TreeNode *> v;
TreeNode * pCur;
};
大神更精简的代码: 经验,把相同功能的代码放在一起可以简化代码。
public class BSTIterator { Stack<TreeNode> stack = null ;
TreeNode current = null ; public BSTIterator(TreeNode root) {
current = root;
stack = new Stack<> ();
} /** @return whether we have a next smallest number */
public boolean hasNext() {
return !stack.isEmpty() || current != null;
} /** @return the next smallest number */
public int next() {
while (current != null) {
stack.push(current);
current = current.left ;
}
TreeNode t = stack.pop() ;
current = t.right ;
return t.val ;
}
}
【leetcode】Binary Search Tree Iterator(middle)的更多相关文章
- 【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 ...
- 【leetcode】Path Sum I & II(middle)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- 【leetcode】Swap Nodes in Pairs (middle)
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- 【leetcode】Linked List Cycle II (middle)
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 【leetcode】Reverse Linked List II (middle)
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- 【leetcode】Minimum Size Subarray Sum(middle)
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 【leetcode】Evaluate Reverse Polish Notation(middle)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 【leetcode】Container With Most Water(middle)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
随机推荐
- R语言 推荐算法 recommenderlab包
recommend li_volleyball 2016年3月20日 library(recommenderlab) library(ggplot2) # data(MovieLense) dim(M ...
- vs 工程连接错误
现象: estMemPool.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: _ ...
- Ubuntu 12 安装 MySQL 5.6.26 及 问题汇总
参考先前的文章:Ubuntu 14 编译安装 PHP 5.4.45 + Nginx 1.4.7 + MySQL 5.6.26 笔记 安装过程: #安装依赖库 sudo apt-get install ...
- python 多线程就这么简单(转)
多线程和多进程是什么自行google补脑 对于python 多线程的理解,我花了很长时间,搜索的大部份文章都不够通俗易懂.所以,这里力图用简单的例子,让你对多线程有个初步的认识. 单线程 在好些年前的 ...
- XH
1. 又到父亲节,那就给老爹做顿饭呗,让他开心开心. 老爸吃了一口我炒的菜,流露出感动的泪花说:儿呀,你能为爸爸做饭,爸爸感到特别开心,但是你这个菜,看在今天是父亲节 我能不能不吃呀! 2. 一哥 ...
- Jquery Ajax调用aspx页面方法
Jquery Ajax调用aspx页面方法 在asp.net webform开发中,用jQuery ajax传值一般有几种玩法 1)普通玩法:通过一般处理程序ashx进行处理: 2)高级玩法:通过as ...
- Java NIO与IO的区别和比较
传统的socket IO中,需要为每个连接创建一个线程,当并发的连接数量非常巨大时,线程所占用的栈内存和CPU线程切换的开销将非常巨大.使用NIO,不再需要为每个线程创建单独的线程,可以用一个含有限数 ...
- JAVA正则表达式:Pattern类与Matcher类详解
java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包.它包括两个类:Pattern和Matcher Pattern 一个Pattern是一个正则表达式经编译后的表 ...
- 160809209_李梦鑫_C语言程序设计实验2+选择结构程序设计_进阶
<C语言程序设计>实验报告 学 号 160809209 姓 名 李梦鑫 专业.班 计科16-2班 学 期 2016-2017 第1学期 指导教师 黄俊莲 吴喆 实验地点 C05 机 ...
- SVN迁移到Git的过程(+ 一些技巧)
SVN迁移到Git的过程(+ 一些技巧) 李顺利 Key Words SVN,Git,Clone,Conversion,Tips,VCS,Pro Git 关于在VCS中SVN和Git之间的迁移(Clo ...