当前位置:网站首页>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
边栏推荐
- 【Kubernetes 系列】一文學會Kubernetes Service安全的暴露應用
- Descriptor implements ORM model
- SD卡報錯“error -110 whilst initialising SD card
- Mysql database operation
- Prototype design
- [ruoyi] ztree custom icon (iconskin attribute)
- Daily question brushing plan-2-13 fingertip life
- 深入探究指针及指针类型
- Single instance mode of encapsulating PDO with PHP in spare time
- Pure QT version of Chinese chess: realize two-man, man-machine and network games
猜你喜欢
Linear programming matlab
[Chongqing Guangdong education] higher mathematics I reference materials of Southwest Petroleum University
Jenkins basic knowledge ----- detailed explanation of 03pipeline code
Analyze 菜单分析
故障分析 | MySQL 耗尽主机内存一例分析
Apt installation ZABBIX
XSS challenges bypass the protection strategy for XSS injection
Huawei, H3C, Cisco command comparison, mind map form from the basic, switching, routing three directions [transferred from wechat official account network technology alliance station]
建模规范:命名规范
JS regular filtering and adding image prefixes in rich text
随机推荐
Fault analysis | analysis of an example of MySQL running out of host memory
解决:AttributeError: ‘str‘ object has no attribute ‘decode‘
Pat 1046 shortest distance (20 points) simulation
深度解析指针与数组笔试题
如何做好功能测试
Installation and use tutorial of cobaltstrike-4.4-k8 modified version
A copy can also produce flowers
Classic interview question [gem pirate]
Pat 1084 broken keyboard (20 points) string find
My C language learning records (blue bridge) -- files and file input and output
MySQL advanced notes
Universal crud interface
Briefly describe the implementation principle of redis cluster
Daily question brushing plan-2-13 fingertip life
C语言sizeof和strlen的区别
手写数据库客户端
Résumé des méthodes de reconnaissance des caractères ocr
微服务注册与发现
Derivation of anti Park transform and anti Clarke transform formulas for motor control
C # create self host webservice