【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). ...
随机推荐
- [译]简单得不得了的教程-一步一步用 NODE.JS, EXPRESS, JADE, MONGODB 搭建一个网站
原文: http://cwbuecheler.com/web/tutorials/2013/node-express-mongo/ 原文的源代码在此 太多的教程教你些一个Hello, World!了, ...
- ASP.NET原理分析
ASP.NET请求与处理全过程分析 1.用户向服务器的某IP端口发送请求,此端口通过Http.sys来管理,请求报文被Http.sys接收,Http.sys在注册表中找能处理这个请求类型的应用程序,最 ...
- js改变HTML元素的值
js改变HTML元素的值(常用,备忘) <!DOCTYPE html> <html> <body> <h1>我的第一段 JavaScript</h ...
- 给一系列的div中的第一个添加class
$(".lb:first").addClass("active")
- Flash+XML前后按钮超酷焦点图,层叠翻转图形
Flash+XML,有“前后”按钮,可以左右点击,支持鼠标滚轮,效果流畅,推荐下载.大图尺寸:680x345 点击下载
- js表单元素checked、radio被选中的几种方式-遁地龙卷风
0.环境 <input type="checkbox" value="lol"/>lol var lol = document.getElemen ...
- 【转】 GridView 72般绝技
说明:准备出一个系列,所谓精髓讲C#语言要点.这个系列没有先后顺序,不过尽量做到精.可能会不断增删整理,本系列最原始出处是csdn博客,谢谢关注. C#精髓 第四讲 GridView 72般绝技 作者 ...
- 深入理解Java虚拟机之读书笔记一 自动内存管理机制
一.运行时数据区域 1.程序计数器是线程的私有空间,每个线程都有.针对线程执行的是Java代码还是Native代码有两种取值,Java代码时:虚拟机字节码指令的地址:Native代码时:计数值为Und ...
- ducument.ready不生效的问题 ruby on rails
rails web app页面之间的跳转的时候使用ducument.ready只有在再次加载的时候才生效, 因为rails用了turbolinks, https://github.com/turbol ...
- Android学习之路书籍推荐
Android开发书籍推荐:从入门到精通系列学习路线书籍介绍 JAVA入门书籍: < Introduction to java programming > < Core java & ...