LeetCode Binary Search Tree Iterator
原题链接在这里:https://leetcode.com/problems/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.
题解:
用一个stack把所有最左边的node压进 stack中。
判断hasNext()时就是看stack 是否为空.
next()返回stack顶部top元素,若是top有右子树,就把右子树的所有最左node压入stack中.
constructor time complexity: O(h).
hashNext time complexity: O(1).
next time complexity: O(h).
Space: O(h), stack 最大为树的高度。
AC Java:
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/ public class BSTIterator {
Stack<TreeNode> stk; public BSTIterator(TreeNode root) {
stk = new Stack<TreeNode>();
while(root != null){
stk.push(root);
root = root.left;
}
} /** @return whether we have a next smallest number */
public boolean hasNext() {
return !stk.isEmpty();
} /** @return the next smallest number */
public int next() {
TreeNode top = stk.pop();
TreeNode rightLeft = top.right;
while(rightLeft != null){
stk.push(rightLeft);
rightLeft = rightLeft.left;
} return top.val;
}
} /**
* Your BSTIterator will be called like this:
* BSTIterator i = new BSTIterator(root);
* while (i.hasNext()) v[f()] = i.next();
*/
类似Binary Tree Inorder Traversal, Inorder Successor in BST.
LeetCode Binary Search Tree Iterator的更多相关文章
- LeetCode: Binary Search Tree Iterator 解题报告
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- LeetCode——Binary Search Tree Iterator
Description: Implement an iterator over a binary search tree (BST). Your iterator will be initialize ...
- [LeetCode] Binary Search Tree Iterator 深度搜索
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- leetcode Binary Search Tree Iterator python
# Definition for a binary tree node # class TreeNode(object): # def __init__(self, x): # self.val = ...
- 【leetcode】Binary Search Tree Iterator
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
- 【LeetCode】173. Binary Search Tree Iterator (2 solutions)
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
- leetcode-173:Binary Search Tree Iterator(Java)
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
- 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
随机推荐
- CKEDITOR使用与配置
安装: 下载CKEDITOR的文件,解压后复制到工程的WEBROOT目录下就OK! 引用CKEDITOR的JS文件: 新建JSP页面,添加其JS文件<script type="text ...
- cvReleaseImage 释放内存出错
cvReleaseImage是OpenCV中C语言库中的释放图片内存的函数,比如我们加载或者克隆了一幅图片,当不需要这幅图片了时,我们为了避免内存泄露,需要释放这些空间,可以参见我之前的博客OpenC ...
- php利用wsh突破函数禁用执行命令(安全模式同理)
php利用wsh突破函数禁用执行命令(安全模式同理) 前提.需要服务器支持wsh.并知道php安装目录 但是php利用wsh执行命令是没有asp的权限高的. 突破代码 <?php $cmd= ...
- 清橙 A1206 小Z的袜子(莫队算法)
A1206. 小Z的袜子 时间限制:1.0s 内存限制:512.0MB 总提交次数:1357 AC次数:406 平均分:46.75 将本题分享到: 查看未格式化的试题 ...
- 暑假训练round2 D: 好序列(Manacher)
Problem 1061: 好序列 Time Limits: 1000 MS Memory Limits: 65536 KB 64-bit interger IO format: %lld ...
- iOS dispatch_source_t的理解
Dispatch Source是GCD中的一个基本类型,从字面意思可称为调度源,它的作用是当有一些特定的较底层的系统事件发生时,调度源会捕捉到这些事件,然后可以做其他的逻辑处理,调度源有多种类型,分别 ...
- [杂谈]交通工具orca card
How and Where to Use the ORCA Card The Microsoft ORCA card provides unlimited rides on all buses, tr ...
- [学习笔记]ESXI学习记录
vmware workstation环境下,可以为虚拟机添加多块网卡.在虚拟机上单击右键,settings 界面,选中network adapter,然后通过custom选项,可以将多个不同的虚拟机使 ...
- swfit-block反向传值
// ViewController.swift // Block import UIKit class ViewController: UIViewController { var myLabel = ...
- 使用Nsight查找CE3的渲染bug
工作临时的接的一个小任务,查找ce3引擎修改后在绘制上出的一点bug 在代码的底层调用代码做了一些修改后,场景里的绘制的问题,因为也是刚接触CE3代码,也只能通过Nsight来查找问题了. 首先用 ...