多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國(guó)內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁(yè) > php開(kāi)源 > php教程 > [LeetCode] Binary Search Tree Iterator

[LeetCode] Binary Search Tree Iterator

來(lái)源:程序員人生   發(fā)布時(shí)間:2015-01-16 08:52:29 閱讀次數(shù):2674次

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.

這是1道很經(jīng)典的題目,考的非遞歸的中序遍歷。其實(shí)這道題等價(jià)于寫(xiě)1個(gè)2叉樹(shù)中序遍歷的迭代器。需要內(nèi)置1個(gè)棧,1開(kāi)始先存儲(chǔ)到最左葉子節(jié)點(diǎn)的路徑。在遍歷的進(jìn)程中,只要當(dāng)前節(jié)點(diǎn)存在右孩子,則進(jìn)入右孩子,存除從此處開(kāi)始到當(dāng)前子樹(shù)里最左葉子節(jié)點(diǎn)的路徑。

public class BSTIterator { Stack<TreeNode> stack; public BSTIterator(TreeNode root) { stack = new Stack<TreeNode>(); 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 node = stack.pop(); int ret = node.val; if (node.right != null) { node = node.right; while (node != null) { stack.push(node); node = node.left; } } return ret; } }

生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 亚洲精品a | 日本在线不卡一区二区 | 中文字幕中韩乱码亚洲大片 | 天堂mv亚洲mv在线播放9蜜 | 亚洲欧美视频一区二区 | 自拍偷拍图 | 久久一区二区三区不卡 | 一级作爱视频 | 波多野结衣 在线资源观看 波多野结衣 一区二区 | 一区二区三区不卡在线观看 | 日日夜夜天天久久 | 中文字幕一区二区在线视频 | 国产亚洲精品资源一区 | 日韩亚洲欧美性感视频影片免费看 | 中文字幕第一页在线 | 国产 日韩 欧美 综合 | 久久a毛片 | 国产一级淫片a免费播放口欧美 | 亚洲免费毛片 | 欧美一级淫片aaaaaaa视频 | 国产精品video | 欧美高清18 | 欧美色交 | 四虎必出精品亚洲高清 | 亚洲视频 欧美视频 | 日本在线视频一区二区三区 | 夜夜影院未满十八勿进 | 亚洲欧美一区二区视频 | 最近中文字幕完整视频大全版 | freexxxhd性欧美 | 琪琪在线观看 | 最近最新中文字幕国语片 | 色综合久久久久久久久五月 | 日韩一区二区三区视频 | 亚洲欧美日韩第一页 | 精品久久久久久中文字幕无碍 | 国产成人综合亚洲亚洲欧美 | 免费的爱爱视频 | 五月婷婷亚洲综合 | 淫片在线观看 | 一本大道高清香蕉中文大在线 |