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

首頁 > 學院 > 開發設計 > 正文

Leetcode 173. Binary Search Tree Iterator

2019-11-10 21:32:40
字體:
來源:轉載
供稿:網友

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(); */
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 灌南县| 万荣县| 青田县| 庄浪县| 称多县| 林甸县| 怀化市| 商水县| 龙川县| 麦盖提县| 淄博市| 洞口县| 襄城县| 嘉荫县| 贵州省| 宁乡县| 喜德县| 南通市| 瓮安县| 葫芦岛市| 贡嘎县| 五寨县| 兴和县| 合川市| 淄博市| 射洪县| 彝良县| 晋宁县| 自贡市| 邯郸市| 宜兰县| 西和县| 丹巴县| 汶川县| 安塞县| 开阳县| 枝江市| 进贤县| 姜堰市| 嘉黎县| 冕宁县|