当前位置:网站首页>Leetcode brush question diary sword finger offer II 055. binary search tree iterator
Leetcode brush question diary sword finger offer II 055. binary search tree iterator
2022-07-28 06:39:00 【JETECHO】
Original link (https://leetcode.cn/problems/kTOapQ/)
List of articles
Title Description
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 .
It can be assumed 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
inputs = [“BSTIterator”, “next”, “next”, “hasNext”, “next”, “hasNext”, “next”, “hasNext”, “next”, “hasNext”]
inputs = [[[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
Ideas
For this tree, you can use a stack to store the values obtained after the first sequence traversal , The value required each time is the value at the top of the stack , When the stack has no value and no value is taken out before the top of the stack , Then it means that there is no number to traverse on the right side of the pointer . For traversal , There is no need to store it all at once , Can maintain this stack , Iterate to the left subtree every time you get the value , Then remove the final value , Then point the pointer of the node to the right subtree , Next time, just keep looking for the left subtree .
Code
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */
class BSTIterator {
TreeNode curr;
Stack<TreeNode> stack;
public BSTIterator(TreeNode root) {
curr=root;
stack=new Stack<>();
}
public int next() {
while(curr!=null) {
stack.add(curr);
curr=curr.left;
}
curr=stack.pop();
int ans=curr.val;
curr=curr.right;
return ans;
}
public boolean hasNext() {
return curr!=null||!stack.isEmpty();
}
}
边栏推荐
猜你喜欢

2022-06-07 ResponseBodyAdvice导致Swagger出现弹框问题

Combine multiple ICs calendars into a single ICs calendar

Hugging face's problem record I
![[c语言]--一步一步实现扫雷小游戏](/img/ee/49ddfcd948ccd5c8c9dec3c48c6112.png)
[c语言]--一步一步实现扫雷小游戏

What's a gift for girls on Chinese Valentine's day? Selfie online and thoughtful gift recommendation

气导贴耳式耳机推荐、不需要佩戴入耳的气传导耳机

用c语言实现三子棋小游戏

AQS之ReentrantLock源码解析

What's a good gift for your girlfriend on Chinese Valentine's day? Boys who can't give gifts, look!

2022年七夕礼物推荐!好看便宜又实用的礼物推荐
随机推荐
Pyppeteer is recognized to bypass detection
AQS之countDownLatch源码分析
2022-07-19 达梦数据库 连接实例、执行脚本、系统命令
[pta ---- traversal of tree]
做气传导耳机最好的是哪家、最好的气传导耳机盘点
Development of clip arbitrage / brick carrying arbitrage system
Valgrind tool
七夕礼物送女生什么好?颜值在线又有心意的礼物推荐
js 变量等于0也等也' '问题
Pyppeter drop-down selenium drop-down
【C笔记】数据类型及存储
2021-11-10
C语言的编译和预处理
QT solves the problem of rebuilding UI files every time they are modified
MFC uses the console to print program information
Leetcode 刷题日记 剑指 Offer II 050. 向下的路径节点之和
SSAO By Computer Shader(三)
藏宝计划TPC系统开发Dapp搭建
2022-06-07 responsebodyadvice caused the spring frame problem in swagger
Execjs call