Leetcode173. Binary Search Tree Iterator二叉搜索树迭代器
实现一个二叉搜索树迭代器。你将使用二叉搜索树的根节点初始化迭代器。
调用 next() 将返回二叉搜索树中的下一个最小的数。
注意: next() 和hasNext() 操作的时间复杂度是O(1),并使用 O(h) 内存,其中 h 是树的高度。
二叉树的中序遍历
class BSTIterator {
public:
stack<TreeNode*> s;
TreeNode *cur;
BSTIterator(TreeNode *root) {
cur = root;
while(cur)
{
s.push(cur);
cur = cur ->left;
}
}
/** @return whether we have a next smallest number */
bool hasNext() {
return !s.empty();
}
/** @return the next smallest number */
int next() {
cur = s.top();
s.pop();
int res = cur ->val;
cur = cur ->right;
while(cur)
{
s.push(cur);
cur = cur ->left;
}
return res;
}
};
Leetcode173. Binary Search Tree Iterator二叉搜索树迭代器的更多相关文章
- 173 Binary Search Tree Iterator 二叉搜索树迭代器
实现一个二叉搜索树迭代器.你将使用二叉搜索树的根节点初始化迭代器.调用 next() 将返回二叉搜索树中的下一个最小的数.注意: next() 和hasNext() 操作的时间复杂度是O(1),并使用 ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- [leetcode]173. Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树
4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [LeetCode] Validate Binary Search Tree 验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- LeetCode 235. Lowest Common Ancestor of a Binary Search Tree (二叉搜索树最近的共同祖先)
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- LeetCode 669. Trim a Binary Search Tree修剪二叉搜索树 (C++)
题目: Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so th ...
随机推荐
- java-day05
数组概念 是一种容器,能够存放多个数据值 特点 多个数据值类型必须统一 是一种引用数据类型 程序运行时,数组长度不可改变 数组初始化 动态初始化格式 数据类型[] 数组名称 = new 数据类型[数组 ...
- 7_1.springboot2.x启动配置原理_1.创建SpringApplication对象
环境准备 springboot2.1.9.idea2019. pom.xml 解析 几个重要的事件回调机制 配置在META-INF/spring.factories ApplicationContex ...
- JPA默认方法查询遇到转JSON的处理
JPA提供的findAll等查询方法在有关联的对象时 比如:在查userInfo @Entity@Table(name = "user_info")public class Use ...
- PCA降维的前世今生
PCA降维的数学原理 PCA(Principal Component Analysis)是一种常用的数据分析方法.PCA通过线性变换将原始数据变换为一组各维度线性无关的表示,可用于提取数据的主要特征分 ...
- 用在 AMD64 上 aria2_1.33.1-1_amd64.deb 的下载页面
用在 AMD64 上 aria2_1.33.1-1_amd64.deb 的下载页面 如果您正在运行 Ubuntu,请尽量使用像 aptitude 或者 synaptic 一样的软件包管理器,代替人工手 ...
- vue 简单留言本
代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- Java虚拟机性能管理神器 - VisualVM(7) 排查JAVA应用程序线程泄漏【转】
Java虚拟机性能管理神器 - VisualVM(7) 排查JAVA应用程序线程泄漏[转] 标签: javajvm线程泄漏 2015-03-11 19:47 1098人阅读 评论(0) 收藏 举报 ...
- 便携版Mysql安装
目录 1.安装 0.Mysql下载地址 1.解压 2.在主目录下新建data和tempData两个文件夹 3.配置环境变量 4.配置my.ini 5.安装服务(管理员模式CMD) 6.清空data文件 ...
- Lock方法是用于数据库的锁机制,
Lock方法是用于数据库的锁机制,如果在查询或者执行操作的时候使用: lock(true); 复制代码 就会自动在生成的SQL语句最后加上 FOR UPDATE或者FOR UPDATE NOWAI ...
- 在Xsheel Linux上安装nodejs和npm
最近window系统转向linux系统开发,linux系统的确适合程序员的开发. 作为前端安装了nodejs和npm,遇到了一些坑,赶紧记录下来 第一种安装方法:安装nodejs : sudo a ...