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

QT implementation outputs relevant information to log file

【C语言】字符串库函数介绍及模拟

set_ multicycle_ path

RayMarching实现体积光渲染

Leetcode brush questions diary sword finger offer II 047. Binary tree pruning
![[untitled]](/img/de/746832bfb3bb79b090215b135b8917.jpg)
[untitled]

QT painting event - set background picture

STM32的IAP跳转相关bug经历

C语言的动态内存管理函数

Problem solving for ACM freshmen in Jiangzhong on October 26
随机推荐
【实现简易版扫雷小游戏】
Leetcode 刷题日记 剑指 Offer II 053. 二叉搜索树中的中序后继
Battle plague Cup -- strange shape
Mysql-8.0.17-winx64 (additional Navicat) manual configuration version installation
OJ 1505 fuse
藏宝计划TPC系统开发Dapp搭建
气传导蓝牙耳机什么牌子好、气传导耳机最好的品牌推荐
[c语言]--一步一步实现扫雷小游戏
七夕送女朋友什么礼物好?不会送礼的男生速看!
[队列,栈的简单应用----包装机]
OJ 1045 反转然后相加
七夕送什么礼物好?小众又高级的产品礼物推荐
【C语言】自定义结构体类型
七夕礼物送女生什么好?颜值在线又有心意的礼物推荐
Combine multiple ICs calendars into a single ICs calendar
什么气传导蓝牙耳机好、配置比较高的气传导耳机推荐
气传导蓝牙耳机哪个好、气传导蓝牙耳机排行榜
Several methods of QT setting loading interface
[c语言]简易通讯录的实现
OJ 1507 deletion problem