LeetCode-173 Binary Search Tree Iterator

Title Description

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.

Calling next() will return the next smallest number in the BST.

 

Subject to the effect

Given a binary search tree (BST), to achieve the two operations based on the search tree:

next (): time complexity O requires minimum value search tree (1), then the next lookup to find a second small value, and so on.

hasnext (): Are there next time to find the minimum (that is, whether the binary search tree has been traversed again)

 

Examples

E1

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

 

Problem-solving ideas

Solution1: Simple preorder traversal binary tree, with a vector value save node may be realized O (1) complexity of operation during operation.

Solution2: Policy Implications LeetCode @ xcv58, the use of a stack to save all of the left child node of the current node, after each operation to save the right child node to node operating in the stack, to achieve space complexity is O (h ) height (h of the tree), but the operation of the query is not O (1).

 

Complexity Analysis

Time complexity: O (1 \ O (h))

Space complexity: O (n \ O (h))

 

Code 1

/**
 * 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) {
        init(root);
    }
    
    void init(TreeNode* root) {
        if(root == NULL)
            return;
        
        init(root->left);
        node.push_back(root->val);
        init(root->right);        
    }
    
    /** @return the next smallest number */
    int next() {
        int res = node[0];
        node.erase(node.begin());
        return res;
    }
    
    /** @return whether we have a next smallest number */
    bool hasNext() {
        return (node.size() > 0 ? true : false);
    }
    
private:
    vector<int> node;
};

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

Code 2

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);
    }
};

 

Guess you like

Origin www.cnblogs.com/heyn1/p/10955195.html