当前位置:网站首页>Leetcode problem solving -- 173 Binary search tree iterator
Leetcode problem solving -- 173 Binary search tree iterator
2022-07-06 03:07:00 【Snowy solitary boat】
Iterative thinking
private TreeNode node;
private Stack<TreeNode> stack;
public BSTIterator(TreeNode root) {
this.node =root;
stack = new Stack<>();
}
public int next() {
while (node !=null){
stack.push(node);
node = node.left;
}
node = stack.pop();
int next = node.val;
node = node.right;
return next;
}
public boolean hasNext() {
return node !=null||!stack.isEmpty();
}
Ideas : The iterative scheme based on the middle order traversal is improved
Every call next Get the value of a node
At the same time, the judgment conditions of the original cycle are extracted to hasNext Method to go
Be careful :
This scheme is based on the understanding of the iterative method of middle order traversal , If a little partner doesn't know , And I want to know more about the explanation idea of middle order traversal iteration , Sure Click here to jump
Other options
In fact, you can recursively traverse the tree once , Put it in an array , Creating index variables can also achieve the above functions ,leetcode The official also has corresponding solutions , But I want to , In fact, iterators are originally what I can do as much as you want , If it is realized in this way , Small amount of data is OK , If it's big data , It will undoubtedly cause bad time loss , So I didn't write it out , Interested students can Click here Jump leetcode Official explanation
边栏推荐
- 张丽俊:穿透不确定性要靠四个“不变”
- Single instance mode of encapsulating PDO with PHP in spare time
- My C language learning record (blue bridge) -- on the pointer
- Microservice registration and discovery
- 原型图设计
- Apt installation ZABBIX
- 主数据管理理论与实践
- Problems encountered in 2022 work IV
- Some problem records of AGP gradle
- [network security interview question] - how to penetrate the test file directory through
猜你喜欢
![[Chongqing Guangdong education] higher mathematics I reference materials of Southwest Petroleum University](/img/0f/520242492524522c887b6576463566.jpg)
[Chongqing Guangdong education] higher mathematics I reference materials of Southwest Petroleum University

Solution: attributeerror: 'STR' object has no attribute 'decode‘

4. File modification

JS regular filtering and adding image prefixes in rich text
![BUUCTF刷题笔记——[极客大挑战 2019]EasySQL 1](/img/37/c38a933ce7fa5d2b8fa597965ffcb2.png)
BUUCTF刷题笔记——[极客大挑战 2019]EasySQL 1

MySQL advanced notes

2022工作中遇到的问题四

Web security SQL injection vulnerability (1)

Maturity of master data management (MDM)

My C language learning record (blue bridge) -- on the pointer
随机推荐
Taobao focus map layout practice
C # create self host webservice
I sorted out a classic interview question for my job hopping friends
Erreur de la carte SD "erreur - 110 whilst initialisation de la carte SD
Atcoder beginer contest 233 (a~d) solution
Huawei, H3C, Cisco command comparison, mind map form from the basic, switching, routing three directions [transferred from wechat official account network technology alliance station]
OCR文字識別方法綜述
JS regular filtering and adding image prefixes in rich text
[Yu Yue education] basic reference materials of digital electronic technology of Xi'an University of Technology
Reverse repackaging of wechat applet
解决:AttributeError: ‘str‘ object has no attribute ‘decode‘
tcpdump: no suitable device found
Linear programming matlab
电机控制反Park变换和反Clarke变换公式推导
Jenkins basic knowledge ----- detailed explanation of 03pipeline code
Redis SDS principle
【paddle】加载模型权重后预测报错AttributeError: ‘Model‘ object has no attribute ‘_place‘
PMP practice once a day | don't get lost in the exam -7.5
IPv6 jobs
XSS challenges bypass the protection strategy for XSS injection