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. 给某个view增加颜色渐变图层

    //给某个view增加颜色透明度渐变图层 - (void) insertTransparentGradient { NSLog(@"%@",NSStringFromCGRect(s ...

  2. 如何在ios手机端的Safari浏览器 中“查看网页源代码”

    在这里给大家分享一个很简单的用苹果手机无需越狱就可以查看网页源代码的方法,不过这个方法只用于苹果手机自带的Safari浏览器 随便添加一个safari 书签 (用于一会改为查看源码功能书签)进入书签 ...

  3. centos install shutter (How to enable Nux Dextop repository on CentOS or RHEL)

    http://ask.xmodulo.com/enable-nux-dextop-repository-centos-rhel.html Question: I would like to insta ...

  4. git svn 简易同时使用

    这个方法适合于新的一个git 仓库.假如你使用的git 是最新版本,git本身提供了 git svn命令. 1. 进入一个空的目录,初始化一个空的git仓库: git svn init svn://x ...

  5. [记录] web icon 字体

    weloveiconfonts  在http://codepen.io/cguillou/pen/jmkfK 中看css发现既然有这样的,yes!

  6. jquery animate 制作简单弹幕

    定位滚动条 $("html,body").animate({scrollTop:$(".middle1").offset().top},1000) 弹幕 < ...

  7. JNDI初认识

    JNDI即Java命名和目录接口,英文全称为Java Naming and Directory Interface,从字面上似乎十分晦涩,下面从理论和实际项目应用方面来阐述. 1.命名:在我们实际生活 ...

  8. spark stream初探

    spark带了一个NetworkWordCount测试程序,用以统计来自某TCP连接的单词输入: /usr/local/spark/bin/run-example streaming.NetworkW ...

  9. PRINTDLG 结构体

    //包含 PrintDlg 函数用来初始化Print Dialog Box的信息,在用户关闭窗口后,返回用户选择的信息typedef struct tagPD { DWORD lStructSize; ...

  10. xadmin学习笔记(二)——改造Django教程实例(1)

    前言 xadmin是基于Python和Django的管理框架,想要能够熟练使用,学习Django是必须的.在学习Django的过程中,不妨用xadmin来验证下新的效果是怎样的.本文就是在学习Djan ...