当前位置:网站首页>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();
}
}
边栏推荐
- 当前学习进度
- pyppeteer 下拉 selenium下拉
- [c语言]--一步一步实现扫雷小游戏
- STM32的IAP跳转相关bug经历
- What is the best and most cost-effective open headset recommended
- OJ 1242 freshman's debut
- OJ 1253 ordering problem
- OJ 1129 分数矩阵
- Leetcode brush question diary sword finger offer II 053. Medium order successor in binary search tree
- Leetcode brush questions diary sword finger offer II 047. Binary tree pruning
猜你喜欢

万字归纳总结并实现各大常用排序及性能对比

七夕礼物送女生什么好?颜值在线又有心意的礼物推荐

SSAO By Computer Shader(一)

Antenna effect solution

Problem solving for ACM freshmen in Jiangzhong on October 26

结构体、位段、联合体(共用体)的大小如何计算

C语言的编译和预处理

What's a good gift for your girlfriend on the Chinese Valentine's day in 2022? Practical and beautiful gift recommendation

AQS之ReentrantLock源码解析

用c语言实现三子棋小游戏
随机推荐
2022-07-17 达梦数据库安装
费马小定理
[哈希表基础知识]
2022-06-07 VI. log implementation
Execjs call
Mysql-8.0.17-winx64 (additional Navicat) manual configuration version installation
2022年七夕礼物推荐!好看便宜又实用的礼物推荐
【动态规划--买卖股票的最佳时期系列】
气传导蓝牙耳机什么牌子好、气传导耳机最好的品牌推荐
OJ 1451 digital games
QT solves the problem of rebuilding UI files every time they are modified
OpenGL development environment configuration [vs2017] + frequently asked questions
AQS之CyclicBarrier源码解析
Battle plague Cup -- strange shape
QT batch operation control and set signal slot
OJ 1507 删数问题
【详解如何一步步实现三子棋】
【二叉树基础知识】
刷题记录----二叉树的层序遍历
水渲染示例