当前位置:网站首页>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
边栏推荐
- Analyze menu analysis
- Software design principles
- MySQL learning notes-10-tablespace recycling
- Universal crud interface
- Installation and use tutorial of cobaltstrike-4.4-k8 modified version
- CSP numeric sort
- Jenkins basic knowledge ----- detailed explanation of 03pipeline code
- Atcoder beginer contest 233 (a~d) solution
- 如何做好功能测试
- XSS challenges bypass the protection strategy for XSS injection
猜你喜欢

My C language learning record (blue bridge) -- on the pointer

2022工作中遇到的问题四

PMP practice once a day | don't get lost in the exam -7.5

Microsoft speech synthesis assistant v1.3 text to speech tool, real speech AI generator
![BUUCTF刷题笔记——[极客大挑战 2019]EasySQL 1](/img/37/c38a933ce7fa5d2b8fa597965ffcb2.png)
BUUCTF刷题笔记——[极客大挑战 2019]EasySQL 1

RobotFramework入门(一)简要介绍及使用

微服务注册与发现
![[kubernetes series] learn the exposed application of kubernetes service security](/img/61/4564230feeb988886fe595e3125ef4.png)
[kubernetes series] learn the exposed application of kubernetes service security

What is the investment value of iFLYTEK, which does not make money?

【指针训练——八道题】
随机推荐
OCR文字识别方法综述
【Kubernetes 系列】一文学会Kubernetes Service安全的暴露应用
【若依(ruoyi)】启用迷你导航栏
tcpdump: no suitable device found
Pure QT version of Chinese chess: realize two-man, man-machine and network games
解决:AttributeError: ‘str‘ object has no attribute ‘decode‘
【Unity3D】GUI控件
My C language learning record (blue bridge) -- on the pointer
多态day02
What are the principles of software design (OCP)
主数据管理(MDM)的成熟度
What is the investment value of iFLYTEK, which does not make money?
C语言sizeof和strlen的区别
Fault analysis | analysis of an example of MySQL running out of host memory
2022工作中遇到的问题四
Spherical lens and cylindrical lens
Introduction to robotframework (II) app startup of appui automation
Sign SSL certificate as Ca
Eight super classic pointer interview questions (3000 words in detail)
Derivation of anti Park transform and anti Clarke transform formulas for motor control