国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Leetcode 173. Binary Search Tree Iterator

2019-11-10 21:53:18
字體:
供稿:網(wǎng)友

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.

s思路: 1. 遍歷樹,用iterative。in-order遍歷的套路。

class BSTIterator {PRivate: stack<TreeNode*> ss; TreeNode* cur;public: BSTIterator(TreeNode *root) { cur=root; while(cur){ ss.push(cur); cur=cur->left; } } /** @return whether we have a next smallest number */ bool hasNext() { return !ss.empty(); } /** @return the next smallest number */ int next() { cur=ss.top(); ss.pop(); int res=cur->val; cur=cur->right; while(cur){ ss.push(cur); cur=cur->left; } return res; }};/** * Your BSTIterator will be called like this: * BSTIterator i = BSTIterator(root); * while (i.hasNext()) cout << i.next(); */
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 东山县| 栖霞市| 厦门市| 三台县| 中西区| 佛坪县| 济宁市| 分宜县| 赫章县| 宁波市| 虎林市| 岳普湖县| 石景山区| 农安县| 正蓝旗| 泉州市| 莲花县| 基隆市| 石渠县| 鄢陵县| 海盐县| 如东县| 小金县| 永福县| 根河市| 德格县| 海口市| 阿克| 鸡东县| 珠海市| 武山县| 江阴市| 巴彦县| 四子王旗| 朝阳市| 酉阳| 辽阳市| 泸水县| 芦溪县| 建始县| 巩留县|