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

详见:https://leetcode.com/problems/binary-search-tree-iterator/description/

Java实现:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class BSTIterator { private Stack<TreeNode> stk=new Stack<TreeNode>();
public BSTIterator(TreeNode root) {
while(root!=null){
stk.push(root);
root=root.left;
}
} /** @return the next smallest number */
public int next() {
TreeNode node=stk.pop();
int val=node.val;
if(node.right!=null){
node=node.right;
while(node!=null){
stk.push(node);
node=node.left;
}
}
return val;
} /** @return whether we have a next smallest number */
public boolean hasNext() {
return !stk.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();
*/

参考:https://www.cnblogs.com/grandyang/p/4231455.html

173 Binary Search Tree Iterator 二叉搜索树迭代器的更多相关文章

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

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

  2. Leetcode173. Binary Search Tree Iterator二叉搜索树迭代器

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

  3. [LeetCode] 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. Win8系统如何在桌面行显示我的电脑

    1 桌面右击-个性化   2 更改桌面图标-然后可以在桌面上显示需要的东西

  2. android动画具体解释六 XML中定义动画

    动画View 属性动画系统同意动画View对象并提供非常多比view动画系统更高级的功能.view动画系统通过改变绘制方式来变换View对象,view动画是被view的容器所处理的,由于View本身没 ...

  3. HBase单机环境搭建

    在搭建HBase单机环境之前,首先你要保证你已经搭建好Java环境: $ java -version java version "1.8.0_51" Java(TM) SE Run ...

  4. WebApi-路由机制 Visual Studio 2015中的常用调试技巧分享

    WebApi-路由机制   一.WebApi路由机制是什么? 路由机制通俗点来说:其实就是WebApi框架将用户在浏览器中输入的Url地址和路由表中的路由进行匹配,并根据最终匹配的路由去寻找并匹配相应 ...

  5. react-grid-layout

    一个好用的拖拽.自适应布局 react 插件 基本使用: // 显示全部 chart 内容区域 import React,{PureComponent} from 'react'; import {R ...

  6. oracle sql 超长报ORA-01460错误

    程序查找数据的时候报错了: ORA-01460: 转换请求无法实施或不合理 这是什么鬼?不合理你就提嘛,报错干什么. 程序原本好好的,现在突然报错了.数据库并没有什么更改. 后来猜测是因为执行的SQL ...

  7. struts2 Action获取表单数据

    1.通过属性驱动式 1.首先设置 表单中的数据的name值 如:<input type="text" name="username" value=&quo ...

  8. c# 字节高低位

    byte n = br.ReadByte(); ; // 高位 var l = n & 0x0f; // 低位

  9. Can't remove netstandard folder from output path (.net standard)

    https://developercommunity.visualstudio.com/content/problem/30940/cant-remove-netstandard-folder-fro ...

  10. [HNOI 2007] 紧急疏散

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1189 [算法] 首先 , 答案具有单调性 , 不妨二分答案” 第mid秒是否可以完成 ...