当前位置:网站首页>Leetcode 173 Binary search tree iterator (2022.06.22)
Leetcode 173 Binary search tree iterator (2022.06.22)
2022-06-23 08:06:00 【ChaoYue_ miku】
Implement a binary search tree iterator class BSTIterator , Represents a binary search tree traversed in middle order (BST) The iterator :
BSTIterator(TreeNode root) initialization BSTIterator An object of class .BST The root node root Will be given as part of the constructor . The pointer should be initialized to a value that does not exist in BST Number in , And the number is less than BST Any element in .
boolean hasNext() If there is a number traversing to the right of the pointer , Then return to true ; Otherwise return to false .
int next() Move the pointer to the right , Then return the number at the pointer .
Be careful , The pointer is initialized to a value that does not exist in BST Number in , So for next() The first call of will return BST The smallest element in .
You can assume next() The call is always valid , in other words , When calling next() when ,BST There is at least one next number in the middle order traversal of .
Example :
Input
[“BSTIterator”, “next”, “next”, “hasNext”, “next”, “hasNext”, “next”, “hasNext”, “next”, “hasNext”]
[[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []]
Output
[null, 3, 7, true, 9, true, 15, true, 20, false]
explain
BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]);
bSTIterator.next(); // return 3
bSTIterator.next(); // return 7
bSTIterator.hasNext(); // return True
bSTIterator.next(); // return 9
bSTIterator.hasNext(); // return True
bSTIterator.next(); // return 15
bSTIterator.hasNext(); // return True
bSTIterator.next(); // return 20
bSTIterator.hasNext(); // return False
Tips :
The number of nodes in the tree is in the range [1, 105] Inside
0 <= Node.val <= 106
Call at most 105 Time hasNext and next operation
Advanced :
Can you design a solution that meets the following conditions ?next() and hasNext() The average time complexity of operation is O(1) , And use O(h) Memory . among h Is the height of the tree .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/binary-search-tree-iterator
C++ Submission :
/** * 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:
TreeNode* cur;
stack<TreeNode*> stk;
public:
BSTIterator(TreeNode* root): cur(root) {
}
int next() {
while (cur != nullptr) {
stk.push(cur);
cur = cur->left;
}
cur = stk.top();
stk.pop();
int ret = cur->val;
cur = cur->right;
return ret;
}
bool hasNext() {
return cur != nullptr || !stk.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(); */
边栏推荐
- 看了5本书,我总结出财富自由的这些理论
- Using jetpack datastore for data storage
- C#打印缩放
- Using the for loop to output an alphabetic triangle
- Mathematical knowledge: Euler function - Euler function
- Openvino series 19 Openvino and paddleocr for real-time video OCR processing
- ArcMap batch delete points closer
- Vs problems when connecting to SQL myconn OPen(); cannot execute
- 启动appium
- MySQL慢查询记录
猜你喜欢
随机推荐
Take you to tiktok. That's it
Interview questions of a company in a certain month of a certain year (1)
Gif verification code analysis
Using jetpack datastore for data storage
Vs problems when connecting to SQL myconn OPen(); cannot execute
Capturing packets to find repeated acks and a large number of TCP retransmissions in TCP sessions -- sack (selective acknowledgement) technology
C# 内存法复制图像bitmap
MIT CMS.300 Session 12 – IDENTITY CONSTRUCTION 虚拟世界中身份认同的建立 part 2
openvino系列 19. OpenVINO 与 PaddleOCR 实现视频实时OCR处理
2 corrections de bogues dans l'outil aquatone
ArcMap batch delete points closer
socket编程——select模型
mysql中多表视图性能疑惑
Analysis of open API design specification
odoo项目 发送信息到微信公众号或企业微信的做法
爬虫框架
Match 56 de la semaine d'acwing [terminé]
Apache Solr 任意文件读取复现
AVL树的实现
imperva-查找正则匹配超时的方法









