当前位置:网站首页>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(); */
边栏推荐
猜你喜欢

Commonly used bypass methods for SQL injection -ctf

启动appium

Tensorboard的使用

通过端口查文件

openvino系列 19. OpenVINO 与 PaddleOCR 实现视频实时OCR处理

QT project error: -1: error: cannot run compiler 'clang++' Output:mingw32-make. exe

Create an orderly sequence table and perform the following operations: 1 Insert element x into the table and keep it in order; 2. find the element with the value of X, and delete it if found; 3. outpu

聊聊服务治理中的路由设计

PHP 文件包含 -ctf

Introduction to Excel VBA and practical examples
随机推荐
Openvino series 19 Openvino and paddleocr for real-time video OCR processing
抓包发现tcp会话中老是出现重复的ack和大量的tcp重传——SACK(Selective Acknowledgment, 选择性确认)技术
Mathematical knowledge: Euler function - Euler function
图像分割-改进网络结构
How do I install MySQL on my computer?
聊聊服务治理中的路由设计
Configuration asmx not accessible
值得反复回味的81句高人高语
[veusz] import 2D data in CSV
Sequence table Curriculum
SQL注入常用到的绕过方式-ctf
11 string function
A record of "from scratch" in college student accounts
开源软件、自由软件、Copyleft、CC都是啥,傻傻分不清楚?
2 corrections de bogues dans l'outil aquatone
数学知识:欧拉函数—欧拉函数
AVL树的实现
生产环境服务器环境搭建+项目发布流程
Quickly delete the node in the code_ modules
正则表达式使用案例