Classical algorithm binary search tree iterator

1. Topic description
Implement a binary search tree iterator. You will use the root node of the binary search tree to initialize the iterator.

Calling next () will return the next smallest number in the binary search tree.
Example:
Insert picture description here
BSTIterator iterator = new BSTIterator (root);
iterator.next (); // returns 3
iterator.next (); // returns 7
iterator.hasNext (); // returns true
iterator.next (); // returns 9
iterator.hasNext (); // returns true
iterator.next (); // returns 15
iterator.hasNext (); // returns true
iterator.next (); // returns 20
iterator.hasNext (); // returns false

prompt:

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

2. My code
/ **

  • Definition for a binary tree node.

  • struct TreeNode {

  • int val;
    
  • TreeNode *left;
    
  • TreeNode *right;
    
  • TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    
  • };
    /
    class BSTIterator {
    public:
    BSTIterator(TreeNode
    root) {
    mPRoot = root;
    }

    /** @return the next smallest number /
    int next() {
    int val = 0;
    TreeNode
    left_tmp = NULL;
    TreeNode* cur_tmp = NULL;
    TreeNode* father_tmp = NULL;

     if(mPRoot->left == NULL){
         val = mPRoot->val;
         mPRoot = mPRoot->right;
     }
     else {
         father_tmp = mPRoot;
         left_tmp = mPRoot->left;
         cur_tmp = left_tmp;
         do {
             cur_tmp = left_tmp->left;
             if(cur_tmp != NULL){
                 father_tmp = left_tmp;
                 left_tmp = cur_tmp;
             }
         }while(cur_tmp != NULL);
    
         val = left_tmp->val;
         father_tmp->left = left_tmp->right;
     }
    
     return val;
    

    }

    /** @return whether we have a next smallest number */
    bool hasNext() {
    if(mPRoot == NULL)
    return false;
    else
    return true;
    }

    TreeNode* mPRoot;
    };

/**

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

3. A good solution on the Internet
a. Organize the data into a continuous one-dimensional array, queue
b. Use stack to perform random pause processing instead of processing all the data first.
4.
Operational efficiency and memory where you can improve The occupancy is not ideal
5. Optimize the code to be simple
/ **

  • Definition for a binary tree node.

  • struct TreeNode {

  • int val;
    
  • TreeNode *left;
    
  • TreeNode *right;
    
  • TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    
  • };
    /
    class BSTIterator {
    public:
    stack<TreeNode
    > tree_stack;

    void INOrderleft(TreeNode* root) {
    while(root != NULL){
    tree_stack.push(root);
    root = root->left;
    };
    }
    BSTIterator(TreeNode* root) {
    INOrderleft(root);
    }

    /** @return the next smallest number /
    int next() {
    TreeNode
    cur_tmp = tree_stack.top();
    tree_stack.pop();
    if(cur_tmp->right != NULL) {
    INOrderleft(cur_tmp->right);
    }
    return cur_tmp->val;
    }

    /** @return whether we have a next smallest number */
    bool hasNext() {
    int size = tree_stack.size();
    if(size > 0)
    return true;
    else
    return false;
    }

};

/**

  • 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 ();
    * /
    6. Thinking about
    the tools provided by STL should be used more
Published 4 original articles · Likes0 · Visits 24

Guess you like

Origin blog.csdn.net/digitaluser/article/details/105606539