【题目】
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.
Callingnext()
will return the next smallest number in the BST.
Note:next()
andhasNext()
should
run in average O(1) time and uses O(h) memory, wherehis the height of the tree.
【分析】
中序遍历
【代码】
/*********************************
* 日期:2015-01-03
* 作者:SJF0115
* 题目: 173.Binary Search Tree Iterator
* 来源:https://oj.leetcode.com/problems/binary-search-tree-iterator/
* 结果:AC
* 来源:LeetCode
* 博客:
**********************************/
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
// 二叉查找树节点
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
// 插入
void TreeInsert(TreeNode*& root,int val){
// 创建新节点
TreeNode* node = new TreeNode(val);
// 空树
if(root == NULL){
root = node;
return;
}//if
TreeNode *pre = NULL;
TreeNode *p = root;
// 寻找插入位置
while(p){
// 父节点
pre = p;
// 沿左子树方向下降
if(val < p->val){
p = p->left;
}//if
// 沿右子树方向下降
else{
p = p->right;
}//else
}//while
// 左子结点处插入
if(val < pre->val){
pre->left = node;
}//if
// 右子结点处插入
else{
pre->right = node;
}//else
}
// 创建二叉查找树
TreeNode* TreeCreate(vector<int> num){
TreeNode *root = NULL;
int len = num.size();
if(len == 0){
return root;
}//if
for(int i = 0;i < len;i++){
TreeInsert(root,num[i]);
}//for
return root;
}
class BSTIterator {
public:
BSTIterator(TreeNode *root) {
TreeNode *p = root;
// 沿左子树下降
while(p){
s.push(p);
p = p->left;
}//while
}
/** @return whether we have a next smallest number */
bool hasNext() {
if(!s.empty()){
return true;
}//if
return false;
}
/** @return the next smallest number */
int next() {
TreeNode *p = NULL;
// 出栈
p = s.top();
s.pop();
int val = p->val;
// 转向右子树
if(p->right){
p = p->right;
// 沿左子树下降
while(p){
s.push(p);
p = p->left;
}//while
}//if
return val;
}
private:
stack<TreeNode*> s;
};
int main() {
// -1代表NULL
vector<int> num = {8,3,1,10,6,14,4,7,13};
TreeNode *root = TreeCreate(num);
BSTIterator bSTIterator = BSTIterator(root);
while(bSTIterator.hasNext()){
cout<<bSTIterator.next()<<endl;
}
}
【代码二】
class BSTIterator {
stack<TreeNode *> myStack;
public:
BSTIterator(TreeNode *root) {
pushAll(root);
}
/** @return whether we have a next smallest number */
bool hasNext() {
return !myStack.empty();
}
/** @return the next smallest number */
int next() {
TreeNode *tmpNode = myStack.top();
myStack.pop();
pushAll(tmpNode->right);
return tmpNode->val;
}
private:
void pushAll(TreeNode *node) {
for (; node != NULL; myStack.push(node), node = node->left);
}
};
分享到:
相关推荐
用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现...
JVM 基础 JAVA 并发 JVM 性能调优 LeetCode 算法 .......
My Solutions to Leetcode Database problems. 我的 Leetcode 数据.zip
原创:leetcode 107. 二叉树的层次遍历 II【队列】* Definition for a binary tree node.
原创:leetcode 111. 二叉树的最小深度记住:最小深度和最大深度方法不同。* Definition for a binary tree node.in
Leetcode 题解.pdf
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101.zip
LeetCode 后端.zip
Leetcode101.zip
Recording personal Java, Python, JavaScript solutions for Leetcode problems. 记录个人 Java, Python, JavaScript 的Leetcode题解.zip
刷leetcode总结.md
原创:leetcode 5. 最长回文子串//寻找以i-1,i为中点偶数长度的回文//寻找以i为中心的奇数长度的回文。
leetcode卡 LeetCode 记录一下再LeetCode上刷的题,坚持每天刷一道吧 2017.06.12 打卡[LeetCode 2. Add Two Numbers], Linked list 2017.06.13 打卡[LeetCode 200. Number of Islands], BFS 2017.06.14 打卡...
My Solutions to Leetcode problems. All solutions support C
原创:leetcode 22.括号生成【回溯】对待这种问题,千万别暴力搜索,那样太笨了。但是这个方法是最容易理解的//回溯法 (后面的括号) 不可以大于 (前面
144.Binary_Tree_Preorder_Traversal二叉树的前序遍历【LeetCode单题讲解系列】
145.Binary_Tree_Postorder_Traversal二叉树的后序遍历【LeetCode单题讲解系列】
94.Binary_Tree_Inorder_Traversal二叉树的中序遍历【LeetCode单题讲解系列】