173. Binary Search Tree Iterator
Design an iterator over a binary search tree with the following rules:
- Elements are visited in ascending order (i.e. an in-order traversal)
next()andhasNext()queries run in O(1) time on average.
Example
Example 1
Input: {10,1,11,#,6,#,12}
Output: [1, 6, 10, 11, 12]
Explanation:
The BST is look like this:
10
/\
1 11
\ \
6 12
You can return the inorder traversal of a BST [1, 6, 10, 11, 12]
Example 2
Input: {2,1,3}
Output: [1,2,3]
Explanation:
The BST is look like this:
2
/ \
1 3
You can return the inorder traversal of a BST tree [1,2,3]
Challenge
Extra memory usage O(h), h is the height of the tree.
Super Star: Extra memory usage O(1)
Method 1:
- /**
- * 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:
- stack<TreeNode*> st;
- public:
- BSTIterator(TreeNode* root) {
- TreeNode *dummy = new TreeNode(0);
- dummy->right = root;
- st.push(dummy);
- next();
- }
- /** @return the next smallest number */
- int next() {
- TreeNode* node = st.top();
- st.pop();
- TreeNode* root = node;
- if (root->right != NULL) {
- root = root->right;
- while (root != NULL) {
- st.push(root);
- root = root->left;
- }
- }
- return node->val;
- }
- /** @return whether we have a next smallest number */
- bool hasNext() {
- return (!st.empty());
- }
- };
- /**
- * 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();
- */
Method 2:
- /**
- * Definition of TreeNode:
- * class TreeNode {
- * public:
- * int val;
- * TreeNode *left, *right;
- * TreeNode(int val) {
- * this->val = val;
- * this->left = this->right = NULL;
- * }
- * }
- * Example of iterate a tree:
- * BSTIterator iterator = BSTIterator(root);
- * while (iterator.hasNext()) {
- * TreeNode * node = iterator.next();
- * do something for node
- */
- class BSTIterator {
- private:
- stack<TreeNode*> st;
- TreeNode* currNode;
- public:
- /*
- * @param root: The root of binary tree.
- */BSTIterator(TreeNode * root) {
- // do intialization if necessary
- currNode = root;
- }
- /*
- * @return: True if there has next node, or false
- */
- bool hasNext() {
- // write your code here
- return currNode != NULL || !st.empty();
- }
- /*
- * @return: return next node
- */
- TreeNode * next() {
- // write your code here
- while (currNode != NULL) {
- st.push(currNode);
- currNode = currNode->left;
- }
- currNode = st.top();
- st.pop();
- TreeNode *node = currNode;
- currNode = currNode->right;
- return node;
- }
- };
Method 3:
- /**
- * Definition of TreeNode:
- * class TreeNode {
- * public:
- * int val;
- * TreeNode *left, *right;
- * TreeNode(int val) {
- * this->val = val;
- * this->left = this->right = NULL;
- * }
- * }
- * Example of iterate a tree:
- * BSTIterator iterator = BSTIterator(root);
- * while (iterator.hasNext()) {
- * TreeNode * node = iterator.next();
- * do something for node
- */
- class BSTIterator {
- private:
- vector<TreeNode*> sorted;
- int index;
- public:
- /*
- * @param root: The root of binary tree.
- */BSTIterator(TreeNode * root) {
- // do intialization if necessary
- index = -1;
- inOrder(root, sorted);
- }
- void inOrder(TreeNode* root, vector<TreeNode*> &sorted) {
- if (!root) return;
- inOrder(root->left, sorted);
- sorted.push_back(root);
- inOrder(root->right, sorted);
- }
- /*
- * @return: True if there has next node, or false
- */
- bool hasNext() {
- // write your code here
- return index + 1 < sorted.size();
- }
- /*
- * @return: return next node
- */
- TreeNode * next() {
- // write your code here
- return sorted[++index];
- }
- };

Do you wish to play 1xbet video slots however don't know the place to start? When I began playing in} in casinos in those days of yore referred to as the 1980s, nearly all slot machines have been three-reel steppers with mechanical reels and handles on the edges. Further requirements for this on line casino under its licences include include proper payouts for winning clients and safeguarding players’ personal information and data. Thanks to encrypted 128-bit SSL know-how, in a position to} safely say that there is positively no Videoslots rip-off afoot. In other words, hackers are losing their time should they attempt to break into this operator’s system. The playthrough rate within the new participant bonus of 35x additionally be|can be} according to the industry norm.
ReplyDelete