博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
173. Binary Search Tree Iterator
阅读量:7078 次
发布时间:2019-06-28

本文共 3447 字,大约阅读时间需要 11 分钟。

题目:

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.

链接: 

题解:

二叉搜索树iterator,要求O(1)的next()和hasNext()。可以用in-order traversal。

再仔细看一看,要求O(h)的memory,这样二刷的时候还要再仔细想一想。还有Morris Traversal要学习.

Time Complexity of next() and hasNext() - O(1), Space Complexity - O(n)

/** * Definition for binary tree * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class BSTIterator {    private Queue
queue; //left, mid ,right public BSTIterator(TreeNode root) { this.queue = new LinkedList<>(); inOrderTraversal(root); } private void inOrderTraversal(TreeNode root) { if(root == null) return; Stack
stack = new Stack<>(); while(root != null || !stack.isEmpty()) { if(root != null) { stack.push(root); root = root.left; } else { root = stack.pop(); queue.offer(root.val); root = root.right; } } } /** @return whether we have a next smallest number */ public boolean hasNext() { return !queue.isEmpty(); } /** @return the next smallest number */ public int next() { if(hasNext()) return queue.poll(); return Integer.MAX_VALUE; }}/** * Your BSTIterator will be called like this: * BSTIterator i = new BSTIterator(root); * while (i.hasNext()) v[f()] = i.next(); */

 

二刷:

一刷写得不智慧。注意这里题目要求是amortized complexity - O(1)。是total expense of one operation。所以我们可以用把in-order traversal分解为左右两部分,然后分别放入stack中,就可以满足题目要求了。

为什么是amortized O(1)呢? 因为在我们遍历整个树的过程中,对每个节点都只push 1次,所以对于遍历n个节点的树,我们总的expense是 O(n),那amortized complexity就等于 O(1)了。

空间复杂度的减少,最好还是用Morris-traversal,下一次一定要好好写一遍。

Java:

Time Complexity: next() - amortized O(1),   hasNext() - O(1), Space Complexity - O(h)

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

 

Reference:

https://leetcode.com/discuss/20001/my-solutions-in-3-languages-with-stack

https://leetcode.com/discuss/20101/ideal-solution-using-stack-java

https://leetcode.com/discuss/30207/my-simple-solution-here

https://leetcode.com/discuss/23721/morris-traverse-solution

http://stackoverflow.com/questions/15079327/amortized-complexity-in-laymans-terms

https://en.wikipedia.org/wiki/Amortized_analysis

转载地址:http://zfvml.baihongyu.com/

你可能感兴趣的文章
调查:Android的领先地位稳固
查看>>
在Maven项目中使用JUnit进行单元测试
查看>>
Docker发布应用程序指南
查看>>
你朋友圈里的广告是怎么做到合你胃口的?
查看>>
#第1天#《C Primer Plus》学习历程
查看>>
为什么说GraphQL可以取代REST API?
查看>>
亚马逊是如何进行软件开发的
查看>>
腾讯开源手游热更新方案,Unity3D下的Lua编程
查看>>
Kafka迎来1.0.0版本,正式告别四位数版本号
查看>>
Chef宣布100%开源,要走红帽模式?\n
查看>>
用实例讲解Spark Sreaming
查看>>
Visual Studio 15.8 Preview 3支持多点编辑功能
查看>>
我们究竟应不应该使用框架?
查看>>
如何用Kotlin Coroutines和Architecture Components进行Android开发?
查看>>
RxJava系列六(从微观角度解读RxJava源码)
查看>>
WWDC 2015大会十大看点总结:Swift要开源了
查看>>
墨瞳漫画 升级vue2 踩坑
查看>>
I/O重定向和管道
查看>>
MindFusion.WinForms Pack v2016.R2发布
查看>>
为什么 NSLog 不支持 Swift 对象
查看>>