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.

最简单的,先把整棵树给遍历了,找到所有的值
 
 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class BSTIterator {
public: vector<int> vals;
int index;
BSTIterator(TreeNode *root) {
DFS(root);
index=-;
} void DFS(TreeNode *root)
{ if(root==NULL) return; DFS(root->left);
vals.push_back(root->val);
DFS(root->right);
} /** @return whether we have a next smallest number */
bool hasNext() { if(index<(int)vals.size()-) return true;
else return false; } /** @return the next smallest number */
int next() {
return vals[++index];
}
}; /**
* Your BSTIterator will be called like this:
* BSTIterator i = BSTIterator(root);
* while (i.hasNext()) cout << i.next();
*/
 
 
运用二叉排序树的性质
 
首先连同root把所有左边节点都push到堆栈中,
然后每当调用next时,返回堆栈栈顶元素,然后把栈顶元素的右孩子,以及他的所有左子树都压入栈即可
 
 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class BSTIterator {
public: stack<TreeNode*> stk;
BSTIterator(TreeNode *root) {
pushLeftNode(root); } void pushLeftNode(TreeNode *root)
{ while(root!=NULL)
{
stk.push(root);
root=root->left;
}
} /** @return whether we have a next smallest number */
bool hasNext() { return !stk.empty();
} /** @return the next smallest number */
int next() { TreeNode * smallestNode=stk.top();
stk.pop();
pushLeftNode(smallestNode->right);
return smallestNode->val;
}
}; /**
* Your BSTIterator will be called like this:
* BSTIterator i = BSTIterator(root);
* while (i.hasNext()) cout << i.next();
*/

【leetcode】Binary Search Tree Iterator的更多相关文章

  1. 【leetcode】Binary Search Tree Iterator(middle)

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  2. 【LeetCode 173】Binary Search Tree Iterator

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  3. leetcode@ [173] Binary Search Tree Iterator (InOrder traversal)

    https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a binary searc ...

  4. ✡ leetcode 173. Binary Search Tree Iterator 设计迭代器(搜索树)--------- java

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  5. leetcode 173. Binary Search Tree Iterator

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  6. 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 ...

  7. [leetcode]173. Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  8. leetCode(24):Binary Search Tree Iterator

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  9. 【LeetCode】173. Binary Search Tree Iterator (2 solutions)

    Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...

随机推荐

  1. cygwin 的不同文件类型显示不同的颜色

    正常情况下,我们的cygwin安装好之后,不管是文件还是文件夹显示的颜色都是一样的,这样在我们平时的工作中很不方便 所以这里记录一下怎么设置成彩色的,其实很简单. 直接修改文件 .bashrc vim ...

  2. 【CodeForces 616D】Longest k-Good Segment

    题意 n个数里,找到最长的一个连续序列使里面最多k个不同的数. 分析 尺取法,每次R++,如果第R个数未出现过,那么不同的数+1,然后这个数的出现次数+1,如果不同的数大于k了,那就要去掉第L个数,直 ...

  3. java操作MySQL数据事务的简单学习

    在执行数据更改操作前使用数据库连接对象调用setAutoCommit方法(conn.setAutoCommit(false)),其参数true或false区别: true:sql命令的提交(commi ...

  4. Kd-tree算法原理

    参考资料: Kd Tree算法原理 Kd-Tree,即K-dimensional tree,是一棵二叉树,树中存储的是一些K维数据.在一个K维数据集合上构建一棵Kd-Tree代表了对该K维数据集合构成 ...

  5. Protocol Buffers(Protobuf) 官方文档--Protobuf语言指南

    Protocol Buffers(Protobuf) 官方文档--Protobuf语言指南 约定:为方便书写,ProtocolBuffers在下文中将已Protobuf代替. 本指南将向您描述如何使用 ...

  6. Matalab IFS分形算法

    IFS 算法代码 function IFS_draw(M,p) N=; :length(p); eval(['a',num2str(k),'=reshape(M(',num2str(k),',:),2 ...

  7. ResultSet

    在Java中,获得ResultSet的总行数的方法有以下几种. 第一种:利用ResultSet的getRow方法来获得ResultSet的总行数 Java代码Statement stmt = con. ...

  8. Android往SD卡上存储文件

    public class DataActivity extends Activity { private EditText filenameText; private EditText content ...

  9. Objective-C 之优雅的命名(转)

    There are only two hard things in Computer Science: cache invalidation and naming things. 在计算机科学中只有两 ...

  10. 优化MySQL,还是使用缓存?读一篇文章有感

    今天我想对一个Greenfield项目上可以采用的各种性能优化策略作个对比.换言之,该项目没有之前决策强加给它的各种约束限制,也还没有被优化过. 具体来说,我想比较的两种优化策略是优化MySQL和缓存 ...