当前位置: 首页 > news >正文

(nice!!!)(LeetCode 面试经典 150 题) 173. 二叉搜索树迭代器 (栈)

题目:173. 二叉搜索树迭代器

在这里插入图片描述
在这里插入图片描述
思路:栈,时间复杂度0(n)。

C++版本:

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class BSTIterator {
public:stack<TreeNode*> st;TreeNode * tr; BSTIterator(TreeNode* root) {tr=root;}int next() {//每次先中序遍历trwhile(tr!=nullptr){st.push(tr);tr=tr->left;}// 确保当前是中序遍历的第一个节点tr=st.top();st.pop();int tmp=tr->val;//保留当前节点的右子树,便于下次接着进行中序遍历tr=tr->right;return tmp;}bool hasNext() {return tr!=nullptr || st.size()!=0 ;}
};/*** Your BSTIterator object will be instantiated and called as such:* BSTIterator* obj = new BSTIterator(root);* int param_1 = obj->next();* bool param_2 = obj->hasNext();*/

JAVA版本:

/*** 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 tr;Deque<TreeNode> st;public BSTIterator(TreeNode root) {tr=root;st=new LinkedList<TreeNode>();}public int next() {while(tr!=null){st.push(tr);tr=tr.left;}tr=st.pop();int tmp=tr.val;tr=tr.right;return tmp;}public boolean hasNext() {return tr!=null || !st.isEmpty() ;}
}/*** Your BSTIterator object will be instantiated and called as such:* BSTIterator obj = new BSTIterator(root);* int param_1 = obj.next();* boolean param_2 = obj.hasNext();*/

GO版本:

/*** Definition for a binary tree node.* type TreeNode struct {*     Val int*     Left *TreeNode*     Right *TreeNode* }*/
type BSTIterator struct {tr *TreeNodest []*TreeNode
}func Constructor(root *TreeNode) BSTIterator {return BSTIterator{tr:root}
}func (this *BSTIterator) Next() int {for this.tr!=nil {this.st=append(this.st,this.tr)this.tr=this.tr.Left}this.tr=this.st[len(this.st)-1];this.st=this.st[:len(this.st)-1]tmp:=this.tr.Valthis.tr=this.tr.Rightreturn tmp
}func (this *BSTIterator) HasNext() bool {return this.tr!=nil || len(this.st)>0
}/*** Your BSTIterator object will be instantiated and called as such:* obj := Constructor(root);* param_1 := obj.Next();* param_2 := obj.HasNext();*/
http://www.xdnf.cn/news/1351549.html

相关文章:

  • 55 C++ 现代C++编程艺术4-元编程
  • 数据结构与算法-字符串、数组和广义表(String Array List)
  • 【Tech Arch】Apache Flume海量日志采集的高速公路
  • 解码LLM量化:深入剖析最常见8位与4位核心算法
  • Mac相册重复照片终结指南:技术流清理方案
  • chromadb使用hugging face模型时利用镜像网站下载注意事项
  • Node.js特训专栏-实战进阶:23. CI/CD流程搭建
  • 通过官方文档详解Ultralytics YOLO 开源工程-熟练使用 YOLO11实现分割、分类、旋转框检测和姿势估计(附测试代码)
  • 优先使用 `delete` 关键字删除函数,而不是将函数声明为 `private` 但不实现 (Effective Modern C++ 条款11)
  • 2025年Java在中国开发语言排名分析报告
  • 深度学习之PyTorch框架(安装,手写数字识别)
  • Redis 从入门到实践:Python操作指南与核心概念解析
  • Redis全面详解:从配置入门到实战应用
  • 联邦学习之----联邦批量归一化(FedBN)
  • 非线性规划学习笔记
  • 【KO】前端面试题一
  • 浮点数比较的致命陷阱与正确解法(精度问题)
  • 【Linux】深度学习Linux下的包管理器yum/apt
  • 自动化知识工作AI代理的工程与产品实现
  • 文献阅读笔记【物理信息神经网络】:Physics-informed neural networks: A deep learning framework...
  • 深入理解 Linux 系统文件 I/O:从 open 到重定向的底层逻辑》
  • CA6150主轴箱系统设计cad+设计说明书
  • Spring:IOC(控制反转 )、DI(依赖注入 )、AOP(通知类型、事务、拦截器)
  • 博士招生 | 美国圣地亚哥州立大学 Yifan Zhang 课题组博士招生,AI 安全领域顶尖平台等你加入!
  • ​崩坏世界观中的安全漏洞与哲学映射:从渗透测试视角解构虚拟秩序的脆弱性​
  • lanczso算法中的额外正交化代码解释
  • Linux问答题:分析和存储日志
  • Leetcode—931. 下降路径最小和【中等】
  • 告别静态网页:我用Firefly AI + Spline,构建次世代交互式Web体验
  • 同类软件对比(一):Visual Studio(IDE) VS Visual Studio Code