Binary Search Tree Iterator

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

Calling next() will return the next smallest number in the BST.

Note: next() and hasNext() should
run in average O(1) time and uses O(h) memory, where h is the height of the tree.

Credits:

Special thanks to @ts for adding this problem and creating all test cases.

要求:写一个二叉查找树,每次返回树中的下一个最小节点

比如上图中的二叉查找树,从根节点开始,依次返回1,3,4,6,7... ...

思路:维护一个栈,先将根结点的左子树全部压栈,每次弹出栈顶元素,若某次弹出的栈顶元素有右子树,比如3,此时需要将以该节点的右子树为根的子树的左子节点全部压栈

/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/ import java.util.Stack;
public class BSTIterator {
Stack<TreeNode> stack = new Stack<TreeNode>(); public BSTIterator(TreeNode root) { while(root != null){
stack.push(root);
root = root.left;
}
} /** @return whether we have a next smallest number */
public boolean hasNext() {
return !stack.isEmpty(); } /** @return the next smallest number */
public int next() {
TreeNode minCurrent = stack.pop();
if(minCurrent.right != null){
TreeNode rightNode = minCurrent.right;
while(rightNode != null){
stack.push(rightNode);
rightNode = rightNode.left;
}
} return minCurrent.val;
}
} /**
* Your BSTIterator will be called like this:
* BSTIterator i = new BSTIterator(root);
* while (i.hasNext()) v[f()] = i.next();
*/

leetcode-173:Binary Search Tree Iterator(Java)的更多相关文章

  1. LeetCode OJ:Binary Search Tree Iterator(二叉搜索树迭代器)

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

  2. 【leetcode】Binary Search Tree Iterator(middle)

    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. 【LeetCode】704. Binary Search 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 线性查找 二分查找 日期 题目地址:https:// ...

  5. 【LeetCode】173. Binary Search Tree Iterator (2 solutions)

    Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...

  6. 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator

    144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

  7. 【leetcode】Binary Search Tree Iterator

    Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...

  8. LeetCode: Binary Search Tree Iterator 解题报告

    Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...

  9. [LeetCode] 272. Closest Binary Search Tree Value II 最近的二叉搜索树的值 II

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

随机推荐

  1. Oracle恢复已删除数据

    Oracle恢复已删除的数据,主要有两种方法:SCN和时间戳. 一.通过SCN恢复删除且已提交的数据 1.获得当前数据库的scn号 select current_scn from v$database ...

  2. nyoj 96 n-1位数(处理前导 0 的情况)

    n-1位数 时间限制:3000 ms  |            内存限制:65535 KB 难度:1   描述 已知w是一个大于10但不大于1000000的无符号整数,若w是n(n≥2)位的整数,则 ...

  3. symonfy 项目根目录下没有 bin/console 文件的解决方法

    “Could not open input file: bin/console” Error comes when try to Run the Symfony Application 在采纳的答案中 ...

  4. h2database源码浅析:事务、两阶段提交

    Transaction Isolation Transaction isolation is provided for all data manipulation language (DML) sta ...

  5. activity调用finish方法理解

    /** * Call this when your activity is done and should be closed. The * ActivityResult is propagated ...

  6. try catch异常捕获

    格式为: try            {                int i = int.Parse(Console.ReadLine());           //容易发生错误的语句    ...

  7. js调用本地 exe

      js方法 function Run(strPath) //only for ie { try { var objShell = new ActiveXObject("wscript.sh ...

  8. C#程序员整理的Unity 3D笔记(十):Unity3D的位移、旋转的3D数学模型

    遇到一个想做的功能,但是实现不了,核心原因是因为对U3D的3D数学概念没有灵活吃透.故再次系统学习之—第三次学习3D数学. 本次,希望实现的功能很简单: 如在小地图中,希望可以动态画出Player当前 ...

  9. 第四篇、CSS选择器

    <html> <head> <meta charset="UTF-8"> <title>CSS选择器</title> & ...

  10. java中实现多态的机制是什么?

    多态性是面向对象程序设计代码重用的一个重要机制,我们曾不只一次的提到Java多态性.在Java运行时多态性:继承和接口的实现一文中,我们曾详细介绍了Java实现运行时多态性的动态方法调度:今天我们再次 ...