leetcode刷题,总结,记录,备忘 173
来源:互联网 发布:如何加强网络文化建设 编辑:程序博客网 时间:2024/06/06 06:56
leetcode173Binary Search Tree Iterator
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.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class BSTIterator {public: BSTIterator(TreeNode *root) { while (root) { st.push(root); root = root->left; } } /** @return whether we have a next smallest number */ bool hasNext() { return !st.empty(); } /** @return the next smallest number */ int next() { TreeNode * temp = st.top(); st.pop(); if (temp->right) { TreeNode * t = temp->right; while (t) { st.push(t); t = t->left; } } return temp->val; }private: stack<TreeNode*> st;};/** * Your BSTIterator will be called like this: * BSTIterator i = BSTIterator(root); * while (i.hasNext()) cout << i.next(); */
0 0
- leetcode刷题,总结,记录,备忘 173
- leetcode刷题。总结,记录,备忘 104
- leetcode刷题。总结,记录,备忘 100
- leetcode刷题。总结,记录,备忘 191
- leetcode刷题。总结,记录,备忘。168
- leetcode刷题, 总结,记录,备忘 171
- leetcode刷题 ,总结,记录,备忘。144
- leetcode刷题,总结,记录, 备忘 141
- leetcode刷题,总结,记录,备忘。116
- leetcode刷题,总结,记录,备忘。96
- leetcode刷题。记录,总结,备忘。217
- leetcode 刷题, 总结,记录,备忘 35
- leetcode刷题,总结,记录,备忘 226
- leetcode刷题,总结,记录,备忘 169
- leetcode刷题,总结,记录,备忘137
- leetcode刷题,总结,记录,备忘83
- leetcode刷题,总结,记录,备忘70
- leetcode刷题,总结,记录,备忘 108
- Unable to start activity com.unionpay.uppay.PayActivity
- Scala教程(十五)Scala的特性逆变与协变
- VC++动态链接库(DLL)编程
- LightOJ - 1032 Fast Bit Calculations(数位DP)
- http://blog.csdn.net/jungsagacity/article/details/7618587
- leetcode刷题,总结,记录,备忘 173
- Mysql常用命令(二)
- 人工智能的技术路线
- 利用bigDecimal直接截取小数位数
- 人工智能课程学习
- 一个空格引起的“惨案”
- GET请求和POST请求剖析
- mysql创建数据库实例和导入数据
- 【转】有关maven by:buzhucele