题目描述

实现一个二叉搜索树迭代器类BSTIterator ,表示一个按中序遍历二叉搜索树(BST)的迭代器:

  • BSTIterator(TreeNode root) 初始化 BSTIterator 类的一个对象。BST 的根节点 root 会作为构造函数的一部分给出。指针应初始化为一个不存在于 BST 中的数字,且该数字小于 BST 中的任何元素。
  • boolean hasNext() 如果向指针右侧遍历存在数字,则返回 true ;否则返回 false 。
  • int next()将指针向右移动然后返回指针处的数字

注意,指针初始化为一个不存在于 BST 中的数字,所以对 next() 的首次调用将返回 BST 中的最小元素。
你可以假设 next() 调用总是有效的,也就是说,当调用 next() 时,BST 的中序遍历中至少存在一个下一个数字。

  • 地址:https://leetcode-cn.com/problems/binary-search-tree-iterator/
  • 2021/03/28

    法一:中序遍历 + 存储

  • 二叉搜索树

  • 中序遍历存储,然后判断输出结果

  • 时间复杂度 O(n)

  • 空间复杂度 O(n)
    ```cpp /**

    • 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 { private: void inorder(TreeNode root, vector& res) { // 左根右

      1. if (!root) {
      2. return;
      3. }
      4. // 一直往左走,然后存起来
      5. inorder(root->left, res);
      6. res.push_back(root->val);
      7. inorder(root->right, res);

      }

      vector inorderTraver(TreeNode* root) {

      1. vector<int> res;
      2. inorder(root, res); // 中序遍历
      3. return res;

      }

      vector arr; int idx;

public: BSTIterator(TreeNode* root): idx(0), arr(inorderTraver(root)) {}

  1. int next() {
  2. return arr[idx++];
  3. }
  4. bool hasNext() { // 有元素就返回 true
  5. return (idx < arr.size());
  6. }

};

/**

  • 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(); */ ```

    法二:迭代(栈)

  • 构造方法:一直往左把根节点和子节点的所有左节点放到栈中;
  • 调用 next() 方法时:弹出栈顶的节点;

    • 如果它有右子树,则对右子树一路到底,把它和它的所有左节点放到栈中。
  • 时间复杂度 O(1)

  • 空间复杂度 O(h)

    1. class BSTIterator {
    2. private:
    3. TreeNode* cur;
    4. stack<TreeNode*> stk;
    5. public:
    6. BSTIterator(TreeNode* root): cur(root) {}
    7. int next() {
    8. while (cur != nullptr) { // 一直往左走
    9. stk.push(cur);
    10. cur = cur->left;
    11. }
    12. cur = stk.top();
    13. stk.pop();
    14. int ret = cur->val;
    15. cur = cur->right;
    16. return ret;
    17. }
    18. bool hasNext() {
    19. return cur != nullptr || !stk.empty();
    20. }
    21. };
    1. # Definition for a binary tree node.
    2. # class TreeNode(object):
    3. # def __init__(self, val=0, left=None, right=None):
    4. # self.val = val
    5. # self.left = left
    6. # self.right = right
    7. class BSTIterator(object):
    8. def __init__(self, root):
    9. self.stack = []
    10. while root:
    11. self.stack.append(root)
    12. root = root.left
    13. def next(self):
    14. cur = self.stack.pop()
    15. node = cur.right
    16. while node: # 对于右子树也是一直往左走
    17. self.stack.append(node)
    18. node = node.left
    19. return cur.val
    20. def hasNext(self):
    21. return len(self.stack) > 0