二叉搜索树迭代器
实现一个二叉搜索树迭代器类 BSTIterator ,表示一个按中序遍历二叉搜索树(BST)的迭代器:
BSTIterator (TreeNode root) 初始化 BSTIterator 类的一个对象。BST 的根节点 root 会作为构造函数的一部分给出。指针应初始化为一个不存在于 BST 中的数字,且该数字小于 BST 中的任何元素。
boolean hasNext() 如果向指针右侧遍历存在数字,则返回 true ;否则返回 false 。
int next()将指针向右移动,然后返回指针处的数字。
注意,指针初始化为一个不存在于 BST 中的数字,所以对 next() 的首次调用将返回 BST 中的最小元素。
你可以假设 next() 调用总是有效的,也就是说,当调用 next() 时,BST 的中序遍历中至少存在一个下一个数字。
思路:
二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 它的左、右子树也分别为二叉排序树。
- 首先构造中序遍历结果。参考 二叉树中序遍历结果
- 然后给出迭代器的两个函数。
- 当然,除了递归的方法外,我们还可以利用栈这一数据结构,通过迭代的方式对二叉树做中序遍历。此时,我们无需预先计算出中序遍历的全部结果,只需要实时维护当前栈的情况即可。
- 采用 Morris 中序遍历求出结果再给出迭代器函数。
// 递归
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class BSTIterator {
// 递归中序遍历
void encounter(TreeNode*, vector<int>&);
// 返回中序遍历结果
vector<int> inorder(TreeNode*);
int it;
vector<int> arr;
public:
BSTIterator(TreeNode* root) : it(0), arr(inorder(root)) { }
int next() {
return arr[it++];
}
bool hasNext() {
return it < arr.size();
}
};
void BSTIterator::encounter(TreeNode* root, vector<int>& vec) {
if(!root) return;
encounter(root->left, vec);
vec.push_back(root->val);
encounter(root->right, vec);
}
vector<int> BSTIterator::inorder(TreeNode* root) {
if(!root) return vector<int>();
vector<int> res;
encounter(root, res);
return res;
}
/**
* Your BSTIterator object will be instantiated and called as such:
* BSTIterator* obj = new BSTIterator(root);
* int param_1 = obj->next();
* bool param_2 = obj->hasNext();
*/
时间复杂度:O(n)
空间复杂度:O(n)
// 迭代
class BSTIterator {
private:
TreeNode* cur;
stack<TreeNode*> stk;
public:
BSTIterator(TreeNode* root): cur(root) {}
int next() {
while (cur) {
stk.push(cur);
cur = cur->left;
}
cur = stk.top();
stk.pop();
int ret = cur->val;
cur = cur->right;
return ret;
}
bool hasNext() {
return cur != 0 || !stk.empty();
}
};
时间复杂度:O(n)
空间复杂度:O(n)
// Morris 中序遍历法
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class BSTIterator {
// Morris 中序遍历
vector<int> inorderTraversal(TreeNode*);
int it;
vector<int> arr;
public:
BSTIterator(TreeNode* root) : it(0), arr(inorderTraversal(root)) { }
int next() {
return arr[it++];
}
bool hasNext() {
return it < arr.size();
}
};
vector<int> BSTIterator::inorderTraversal(TreeNode* root) {
vector<int> res;
TreeNode *predecessor = 0; // 初始化一个树节点
while (root) {
if (root->left) {
// predecessor 节点就是当前 root 节点向左走一步,然后一直向右走至无法走为止
predecessor = root->left;
while (predecessor->right && predecessor->right != root) {
predecessor = predecessor->right;
}
// 让 predecessor 的右指针指向 root,继续遍历左子树
if (predecessor->right == 0) {
predecessor->right = root;
root = root->left;
}
// 说明左子树已经访问完了,我们需要断开链接
else {
res.push_back(root->val);
predecessor->right = nullptr;
root = root->right;
}
}
// 如果没有左孩子,则直接访问右孩子
else {
res.push_back(root->val);
root = root->right;
}
}
return res;
}
/**
* Your BSTIterator object will be instantiated and called as such:
* BSTIterator* obj = new BSTIterator(root);
* int param_1 = obj->next();
* bool param_2 = obj->hasNext();
*/
时间复杂度:O(n)
空间复杂度:O(1)