173. Binary Search Tree Iterator
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.
Note: next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree.
S: BST in order traversal
- create empty stack "nodes"
- initiate current node "node" as root;
- push current node to stack and set cur node to node->left until node is empty
- if cur node is not empty, go to 3, else: cur node = stack.top(), stack.pop(), print cur node value and set node = node->right
- if stack.emty and node = NULL, we are done.
time complexity of next() is O(n) but amortized O(1) time, since next got called n times at most, each node at most get visited twice. space complexity O(h)h is the depth of the tree
class BSTIterator {
public:
BSTIterator(TreeNode *root) {
node = root;
}
/** @return whether we have a next smallest number */
bool hasNext() {
return (!nodes.empty() || node);
}
/** @return the next smallest number */
int next() {
TreeNode *cur;
while(node) {
nodes.push(node);
node = node->left;
}
node = nodes.top();
nodes.pop();
cur = node;
node = node->right;
return cur->val;
}
private:
TreeNode *node;
stack<TreeNode*> nodes;
};
/**
* Your BSTIterator will be called like this:
* BSTIterator i = BSTIterator(root);
* while (i.hasNext()) cout << i.next();
*/
501. Find Mode in Binary Search Tree
Given a binary search tree (BST) with duplicates, find all themode(s)(the most frequently occurred element) in the given BST.
Assume a BST is defined as follows:
- The left subtree of a node contains only nodes with keys less than or equal to the node's key.
- The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
- Both the left and right subtrees must also be binary search trees.
For example:
Given BST[1,null,2,2]
,
1
\
2
/
2
return[2]
.
Note:If a tree has more than one mode, you can return them in any order.
Follow up:Could you do that without using any extra space? (Assume that the implicit stack space incurred due to recursion does not count).
S: inorder traversal O(n)
中序遍历,计算最大频率。因为需要O(1)space,第二次中序遍历找到结果
class Solution {
public:
vector<int> findMode(TreeNode* root) {
int mode = 0, count = 0;
findModeDfs(root, nullptr, mode, count);
vector<int> result;
count = 0;
getMode(root, nullptr, mode, count, result);
return result;
}
TreeNode* findModeDfs(TreeNode* root, TreeNode* pre, int& mode, int& count) {
if (!root) return nullptr;
if (root->left) pre = findModeDfs(root->left, pre, mode, count);
count = (pre && pre->val == root->val)? count + 1 : 1;
mode = max(mode, count);
return root->right? findModeDfs(root->right,root, mode, count) : root;
}
TreeNode* getMode(TreeNode* root, TreeNode* pre, int mode, int& count, vector<int>& result) {
if (!root) return nullptr;
if (root->left) pre = getMode(root->left, pre, mode, count, result);
count = pre && pre->val == root->val? count + 1 : 1;
if (mode == count) result.push_back(root->val);
return root->right? getMode(root->right, root, mode, count, result) : root;
}
};