Skip to content

Commit 0dc3bec

Browse files
committed
update 173
1 parent cdfdf55 commit 0dc3bec

File tree

4 files changed

+20
-24
lines changed

4 files changed

+20
-24
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
4242
| [--] | [grind75.md](./list/grind75.md) | 64/75 | - |
4343
| [--] | [leetcode-lee215.md](./list/leetcode-lee215.md) | 2/34 | - |
4444
| [--] | [neetcode150.md](./list/neetcode150.md) | 118/150 | - |
45+
| [] | [9c-advanced.md](./list/9c-advanced.md) | 74/91 | 17 vips |
4546
| [] | [9c-dp.md](./list/9c-dp.md) | 42/45 | 3 vips |
4647
| [] | [geekbang.md](./list/geekbang.md) | 55/55 | - |
4748
| [] | [leetcode101.md](./list/leetcode101.md) | 183/184 | 1 vip |
48-
| [🔲] | [9c-advanced.md](./list/9c-advanced.md) | 73/91 | 17 vips |
4949
| [🔲] | [leetcode-contest.md](./list/leetcode-contest.md) | 10/40 | - |
5050
| [🔲] | [leetcode-google.md](./list/leetcode-google.md) | 0/7 | - |
5151

leetcode/173.binary-search-tree-iterator.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,28 @@
6060
* };
6161
*/
6262
class BSTIterator {
63-
private:
64-
TreeNode* node;
65-
stack<TreeNode*> st;
6663
public:
64+
TreeNode *cur;
65+
stack<TreeNode *> st;
6766
BSTIterator(TreeNode* root) {
68-
node = root;
67+
cur = root;
6968
}
7069

7170
int next() {
72-
while (node) {
73-
st.push(node);
74-
node = node->left;
71+
while (cur) {
72+
st.push(cur);
73+
cur = cur->left;
7574
}
7675

77-
node = st.top();
76+
cur = st.top();
7877
st.pop();
79-
int res = node->val;
80-
node = node->right;
78+
int res = cur->val;
79+
cur = cur->right;
8180
return res;
8281
}
8382

8483
bool hasNext() {
85-
return (st.size() > 0 || node);
84+
return (cur || !st.empty());
8685
}
8786
};
8887

leetcode/173.binary-search-tree-iterator.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,21 @@
5757
class BSTIterator:
5858

5959
def __init__(self, root: Optional[TreeNode]):
60-
self.node = root
60+
self.cur = root
6161
self.stack = []
6262

6363
def next(self) -> int:
64-
while(self.node):
65-
self.stack.append(self.node)
66-
self.node = self.node.left
67-
68-
self.node = self.stack.pop()
69-
res = self.node.val
70-
self.node = self.node.right
64+
while (self.cur):
65+
self.stack.append(self.cur)
66+
self.cur = self.cur.left
7167

68+
self.cur = self.stack.pop()
69+
res = self.cur.val
70+
self.cur = self.cur.right
7271
return res
7372

7473
def hasNext(self) -> bool:
75-
return len(self.stack) > 0 or self.node is not None
76-
74+
return (self.cur is not None or len(self.stack) > 0)
7775

7876
# Your BSTIterator object will be instantiated and called as such:
7977
# obj = BSTIterator(root)

list/9c-advanced.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,4 @@
107107
88. https://www.lintcode.com/problem/flatten-list/
108108
89. https://leetcode.com/problems/flatten-nested-list-iterator/
109109
90. https://www.lintcode.com/problem/flatten-2d-vector/
110-
111-
- https://leetcode.com/problems/binary-search-tree-iterator/
110+
91. https://leetcode.com/problems/binary-search-tree-iterator/

0 commit comments

Comments
 (0)