[LeetCode] C++: Intermediate Problem-Tree 173. Binary Search Tree Iterator

173. Binary Search Tree Iterator

Medium difficulty 322

Implement a binary search tree iterator. You will initialize the iterator with the root node of the binary search tree.

The call  next() will return the next smallest number in the binary search tree.

 

Example:

BSTIterator iterator = new BSTIterator(root); 
iterator.next(); // returns 3 
iterator.next(); // returns 7 
iterator.hasNext(); // returns true 
iterator.next(); // returns 9 
iterator .hasNext(); // return true 
iterator.next(); // return 15 
iterator.hasNext(); // return true 
iterator.next(); // return 20 
iterator.hasNext(); // return false

 

prompt:

  • next()hasNext() The time complexity of the  sum  operation is O(1) and uses O( h ) memory, where  is the height of the tree.
  • You can assume that the  next() call is always valid, that is, when the call  next() is made, there is at least one next smallest number in the BST.

1. In-order traversal + array

/**
 * 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:
    vector<int> vec;
    int index = 0;
public:
    BSTIterator(TreeNode* root) {
        midTraverse(root, vec);
    }
    
    int next() {
        return vec[index++];
    }

    // hasNext() 函数的作用是:返回一个布尔值,表示二叉搜索树中是否还有元素
    bool hasNext() {
        return index != vec.size();
    }

    void midTraverse(TreeNode* root, vector<int>& res){
        if(root == nullptr){
            return ;
        }
        midTraverse(root->left, res);
        res.push_back(root->val);
        midTraverse(root->right, 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();
 */

2. Stack + iteration + middle order traversal

This question uses an iterative method to think more than usual iteration. In the next function, it seems that the middle-order traversal can not be the middle-order traversal. Therefore, the next value obtains the smaller node value after the middle-order traversal, so

/**
 * 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 {
public:
    BSTIterator(TreeNode* root) {
        p = root;
    }
    
    int next() {
        hasNext();
        int res;
        while(p != nullptr || !stk.empty()){
            if(p != nullptr){
                stk.push(p);
                p = p->left;
            }else{
                p = stk.top();
                stk.pop();
                res = p->val;
                p = p->right;
                break;
            }
        }
        return res;
    }
    
    bool hasNext() {
        return p || !stk.empty();
    }
private:
    TreeNode* p;
    stack<TreeNode*> stk;
};

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

Get next() like this, remember, every time you execute next, you get the sequence value after the middle order traversal

    int next() {
        hasNext();
        int res;
        while(p != nullptr || !stk.empty()){
            while(p != nullptr){
                stk.push(p);
                p = p->left;
            }
            if(p == nullptr){
                p = stk.top();
                stk.pop();
                res = p->val;
                p = p->right;
                break;
            }
        }
        return res;
    }

 

Guess you like

Origin blog.csdn.net/weixin_44566432/article/details/113726485