【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). ...
随机推荐
- 通过Canvas及File API缩放并上传图片完整示例
<!DOCTYPE html> <html> <head> <title>通过Canvas及File API缩放并上传图片</title> ...
- QT中Sqlite的使用
环境: 静态编译过sqlite 步骤: 1.C++链接器中加入Sqlite.lib,然后在测试一下是否能正常加载Sqlite驱动 #include<QtPlugin> Q_IMPORT_P ...
- PHP协程 详解
[开源中国] PHP 使用协同程序实现合作多任务 [风雪之隅] 在PHP中使用协程实现多任务调度
- Swift-打开其它Storyboard中的自定义模态窗口
本文的方法针对OS X应用开发. 如果想在某个ViewController中,用模态窗口的方式,打开某个Storyboard中定义的WindowController.可用以下方式. let story ...
- discuz个人空间主题列表 图片模式实现方法
discuz X3空间主题列表 图片展现模式,discuz实现个人空间主题列表调用图片模式,discuz home图片列表 如果需要实现该呈现方式,我们需要首先了解discuz封面图片存储原理:dis ...
- APPCAN MAS接口之AJAX
1.打开APPCAN IDE,文件→新建→MAS服务 2.新建MAS项目 3.新建MAS接口,访问地址http://mobile.163.com/special/chuangye/ 4.修改if_cy ...
- 一次失败的动态转换bean的尝试与思考
前因 公司规范确定不允许使用反射类的进行属性bean的拷贝了,只允许手动的get/set,可以猜到这样定义的原因是制定规范的同事认为反射性能低,虽然写get/set慢点好在性能高.平时开发的时候也是迫 ...
- 实现Android的不同精度的定位(基于网络和GPS)
解决方案: 实现Android的不同精度的定位(基于网络和GPS) Android中的定位服务的相关类基本上都在android.location包中,其中位置服务管理器(LocationManager ...
- iOS开发——UI进阶篇(十二)事件处理,触摸事件,UITouch,UIEvent,响应者链条,手势识别
触摸事件 在用户使用app过程中,会产生各种各样的事件 一.iOS中的事件可以分为3大类型 触摸事件加速计事件远程控制事件 响应者对象在iOS中不是任何对象都能处理事件,只有继承了UIResponde ...
- leetcode 解题报告 Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...