leetcode 173. 二叉搜索樹迭代器

實現一個二叉搜索樹迭代器類BSTIterator ,表示一個按中序遍歷二叉搜索樹(BST)的迭代器:
BSTIterator(TreeNode root) 初始化 BSTIterator 類的一個對象。BST 的根節點 root 會作為構造函數的一部分給出。指針應初始化為一個不存在于 BST 中的數字,且該數字小于 BST 中的任何元素。
boolean hasNext() 如果向指針右側遍歷存在數字,則返回 true ;否則返回 false 。
int next()將指針向右移動,然后返回指針處的數字。
注意,指針初始化為一個不存在于 BST 中的數字,所以對 next() 的首次調用將返回 BST 中的最小元素。

你可以假設 next() 調用總是有效的,也就是說,當調用 next() 時,BST 的中序遍歷中至少存在一個下一個數字。

示例:

輸入
[“BSTIterator”, “next”, “next”, “hasNext”, “next”, “hasNext”, “next”, “hasNext”, “next”, “hasNext”]
[[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []]
輸出
[null, 3, 7, true, 9, true, 15, true, 20, false]

解釋
BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]);
bSTIterator.next(); // 返回 3
bSTIterator.next(); // 返回 7
bSTIterator.hasNext(); // 返回 True
bSTIterator.next(); // 返回 9
bSTIterator.hasNext(); // 返回 True
bSTIterator.next(); // 返回 15
bSTIterator.hasNext(); // 返回 True
bSTIterator.next(); // 返回 20
bSTIterator.hasNext(); // 返回 False

解題思路

使用棧實現中序遍歷
當前遍歷的節點出棧時,將它右節點以及右節點上的所有子節點入棧,實現中序遍歷

代碼

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/class BSTIterator {Stack<TreeNode> stack=new Stack<>();public BSTIterator(TreeNode root) {while (root!=null){stack.add(root);root=root.left;}}public int next() {TreeNode pop = stack.pop();TreeNode right = pop.right;while (right!=null){stack.add(right);right=right.left;}return pop.val;}public boolean hasNext() {return !stack.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();*/

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391540.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391540.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391540.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

jyputer notebook 、jypyter、IPython basics

1 、修改jupyter默認工作目錄&#xff1a;打開cmd&#xff0c;在命令行下指定想要進的工作目錄&#xff0c;即鍵入“cd d/ G:\0工作面試\學習記錄”標紅部分是想要進入的工作目錄。 2、Tab補全 a、在命令行輸入表達式時&#xff0c;按下Tab鍵即可為任意變量&#xff08;對象、…

cookie和session(1)

cookie和session 1.cookie產生 識別用戶 HTTP是無狀態協議&#xff0c;這就回出現這種現象&#xff1a;當你登錄一個頁面&#xff0c;然后轉到登錄網站的另一個頁面&#xff0c;服務器無法認識到。或者說兩次的訪問&#xff0c;服務器不能認識到是同一個客戶端的訪問&#xff0…

熊貓數據集_為數據科學拆箱熊貓

熊貓數據集If you are already familiar with NumPy, Pandas is just a package build on top of it. Pandas provide more flexibility than NumPy to work with data. While in NumPy we can only store values of single data type(dtype) Pandas has the flexibility to st…

2018年,你想從InfoQ獲取什么內容?丨Q言Q語

- Q 言 Q 語 第 三 期 - Q言Q語是 InfoQ 推出的最新板塊&#xff0c; 旨在給所有 InfoQer 一個展示觀點的平臺。 每期一個主題&#xff0c; 不扣帽子&#xff0c;不論對錯&#xff0c;不看輸贏&#xff0c; 只愿跟有趣的靈魂相遇。 本期話題&#xff1a; 2018年&#xff0c;你想…

特征阻抗輸入阻抗輸出阻抗_軟件阻抗說明

特征阻抗輸入阻抗輸出阻抗by Milan Mimica米蘭米米卡(Milan Mimica) 軟件阻抗說明 (Software impedance explained) 數據處理組件之間的阻抗不匹配 (The impedance mismatch between data processing components) It all starts with the simplest signal-processing diagram …

數學建模3

數學建模3 轉載于:https://www.cnblogs.com/Forever77/p/11423169.html

leetcode 190. 顛倒二進制位(位運算)

顛倒給定的 32 位無符號整數的二進制位。 提示&#xff1a; 請注意&#xff0c;在某些語言&#xff08;如 Java&#xff09;中&#xff0c;沒有無符號整數類型。在這種情況下&#xff0c;輸入和輸出都將被指定為有符號整數類型&#xff0c;并且不應影響您的實現&#xff0c;因…

JAVA基礎——時間Date類型轉換

在java中有六大時間類&#xff0c;分別是&#xff1a; 1、java.util包下的Date類&#xff0c; 2、java.sql包下的Date類&#xff0c; 3、java.text包下的DateFormat類&#xff0c;&#xff08;抽象類&#xff09; 4、java.text包下的SimpleDateFormat類&#xff0c; 5、java.ut…

LeetCode第五天

leetcode 第五天 2018年1月6日 22.(566) Reshape the Matrix JAVA class Solution {public int[][] matrixReshape(int[][] nums, int r, int c) {int[][] newNums new int[r][c];int size nums.length*nums[0].length;if(r*c ! size)return nums;for(int i0;i<size;i){ne…

matplotlib可視化_使用Matplotlib改善可視化設計的5個魔術技巧

matplotlib可視化It is impossible to know everything, no matter how much our experience has increased over the years, there are many things that remain hidden from us. This is normal, and maybe an exciting motivation to search and learn more. And I am sure …

adb 多點觸碰_無法觸及的神話

adb 多點觸碰On Twitter, in Slack, on Discord, in IRC, or wherever you hang out with other developers on the internet, you may have heard some formulation of the following statements:在Twitter上&#xff0c;在Slack中&#xff0c;在Discord中&#xff0c;在IRC…

robot:循環遍歷數據庫查詢結果是否滿足要求

使用list類型變量{}接收查詢結果&#xff0c;再for循環遍歷每行數據&#xff0c;取出需要比較的數值 轉載于:https://www.cnblogs.com/gcgc/p/11424114.html

leetcode 74. 搜索二維矩陣(二分)

編寫一個高效的算法來判斷 m x n 矩陣中&#xff0c;是否存在一個目標值。該矩陣具有如下特性&#xff1a; 每行中的整數從左到右按升序排列。 每行的第一個整數大于前一行的最后一個整數。 示例 1&#xff1a; 輸入&#xff1a;matrix [[1,3,5,7],[10,11,16,20],[23,30,34…

rm命令

命令 ‘rm’ &#xff08;remove&#xff09;&#xff1a;刪除一個目錄中的一個或多個文件或目錄&#xff0c;也可以將某個目錄及其下屬的所有文件及其子目錄均刪除掉 語法&#xff1a;rm&#xff08;選項&#xff09;&#xff08;參數&#xff09; 默認會提示‘是否’刪除&am…

javascript消除字符串兩邊空格的兩種方式,面向對象和函數式編程。python oop在調用時候的優點...

主要是javascript中消除字符串空格&#xff0c;比較兩種方式的不同 //面向對象&#xff0c;消除字符串兩邊空格 String.prototype.trim function() { return this.replace(/(^\s*)|(\s*$)/g, ""); };//去左右空格的函數; function trim(s){return s.replace(/(^\s*)…

如何使用Retrofit,OkHttp,Gson,Glide和Coroutines處理RESTful Web服務

Kriptofolio應用程序系列-第5部分 (Kriptofolio app series — Part 5) These days almost every Android app connects to internet to get/send data. You should definitely learn how to handle RESTful Web Services, as their correct implementation is the core knowle…

leetcode 90. 子集 II(回溯算法)

給你一個整數數組 nums &#xff0c;其中可能包含重復元素&#xff0c;請你返回該數組所有可能的子集&#xff08;冪集&#xff09;。 解集 不能 包含重復的子集。返回的解集中&#xff0c;子集可以按 任意順序 排列。 示例 1&#xff1a; 輸入&#xff1a;nums [1,2,2] 輸…

robot:linux下安裝robot環境

https://www.cnblogs.com/lgqboke/p/8252488.html 轉載于:https://www.cnblogs.com/gcgc/p/11425588.html

感知器 機器學習_機器學習感知器實現

感知器 機器學習In this post, we are going to have a look at a program written in Python3 using numpy. We will discuss the basics of what a perceptron is, what is the delta rule and how to use it to converge the learning of the perceptron.在本文中&#xff0…

JS解析格式化Json插件,Json和XML互相轉換插件

Json對象轉換為XML字符串插件 http://www.jsons.cn/Down/jquery.json2xml.js var xml_content $.json2xml(json_object);XML字符串轉換為Json對象插件 http://www.jsons.cn/Down/jquery.xml2json.js var json_obj $.xml2json(xml_content);json序列化和反序列化方法插件 …