实现一个二叉搜索树迭代器。你将使用二叉搜索树的根节点初始化迭代器。

调用 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二叉搜索树迭代器的更多相关文章

  1. 173 Binary Search Tree Iterator 二叉搜索树迭代器

    实现一个二叉搜索树迭代器.你将使用二叉搜索树的根节点初始化迭代器.调用 next() 将返回二叉搜索树中的下一个最小的数.注意: next() 和hasNext() 操作的时间复杂度是O(1),并使用 ...

  2. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  3. [leetcode]173. Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  4. [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 ...

  5. [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 ...

  6. [LeetCode] Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  7. [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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. wangEditor富文本框——例

    官方文档:http://www.wangeditor.com/ 效果 html <!DOCTYPE html> <html> <head> <meta cha ...

  2. MapReduce模型简介

  3. JS数组 了解成员数量(数组属性length) myarr.length

    了解成员数量(数组属性length) 如果我们想知道数组的大小,只需引用数组的一个属性length.Length属性表示数组的长度,即数组中元素的个数. 语法: myarray.length; //获 ...

  4. 调整element-ui中多个button处于同一行

    参考: https://element.eleme.cn/#/zh-CN/component/dropdown <el-row> <el-button-group style=&qu ...

  5. wordpress jwt-auth 多语言 jwt_auth_bad_iss的解决方法

    因为目前处理的 wordpress 网站使用了,使用 qtranslate-x 多语言插件 JWT Authentication for WP REST API 插件 rest api 登录 调用wp ...

  6. HTML - 表格标签相关

    <html> <head></head> <body> <!-- table (表格) border : 表格的边框 width : 表格的宽 h ...

  7. 数论,质因数,gcd——cf1033D 好题!

    直接筛质数肯定是不行的 用map<ll,ll>来保存质因子的指数 考虑只有3-5个因子的数的组成情况 必定是a=pq or a=p*p or a=p*p*p or a=p*p*p*p 先用 ...

  8. JS 计算时间范围,最近一周、一个月

    //最近一周 getDay(-7) 返回的是距离当前日期的一周后的时间//一月 getDay(-30)//一年 getDay(-365) function getDay(day){ var today ...

  9. 菜鸟nginx源码剖析数据结构篇(五) 基数树 ngx_radix_tree_t[转]

    菜鸟nginx源码剖析数据结构篇(五) 基数树 ngx_radix_tree_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blo ...

  10. 深入浅出 Java Concurrency (29): 线程池 part 2 Executor 以及Executors[转]

    Java里面线程池的顶级接口是Executor,但是严格意义上讲Executor并不是一个线程池,而只是一个执行线程的工具.真正的线程池接口是ExecutorService. 下面这张图完整描述了线程 ...