###### 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 {
stack<TreeNode*> qu;
public:
BSTIterator(TreeNode *root) {
push_in_stack(root);
}

/** @return whether we have a next smallest number */
bool hasNext() {
return !qu.empty();

}

/** @return the next smallest number */
int next() {
TreeNode *tmpNode = qu.top();
qu.pop();
push_in_stack(tmpNode->right);
return tmpNode->val;
}
private:
void push_in_stack(TreeNode* node)
{
while(node)
{
qu.push(node);
node=node->left;
}
}
};

/**
* Your BSTIterator will be called like this:
* BSTIterator i = BSTIterator(root);
* while (i.hasNext()) cout << i.next();
*/

while (i.hasNext()) cout << i.next();

#### leetcode-173：Binary Search Tree Iterator（Java）

2015-08-18 10:58:59

#### leetcode 173: Binary Search Tree Iterator

2015-01-02 12:44:56

#### Leetcode: Binary Search Tree Iterator

2015-01-03 22:13:09

#### [C++]LeetCode: 93 Binary Search Tree Iterator （经典题，非递归的中序遍历）

2015-01-14 16:32:34

#### Leetcode 173 Binary Search Tree Iterator 二叉查找树迭代器

2015-08-04 18:27:43

#### Binary Search Tree Iterator: O(1)时间复杂度，O(h)空间复杂度

2015-06-30 17:35:57

#### 实现二分查找树的迭代器（Binary Search Tree Iterator ）

2015-05-09 16:10:32

#### Binary Search Tree Iterator 二叉搜索树的遍历

2015-05-28 22:19:41

#### [leetcode] 173. Binary Search Tree Iterator 解题报告

2016-01-12 01:29:50

#### [LeetCode]Binary Search Tree Iterator，解题报告

2015-02-09 21:11:40

## 不良信息举报

Binary Search Tree Iterator