solution.md 5.8 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1
# 二叉搜索树迭代器
2

每日一练社区's avatar
每日一练社区 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
实现一个二叉搜索树迭代器类<code>BSTIterator</code> ,表示一个按中序遍历二叉搜索树(BST)的迭代器:
<div class="original__bRMd">
<div>
<ul>
	<li><code>BSTIterator(TreeNode root)</code> 初始化 <code>BSTIterator</code> 类的一个对象。BST 的根节点 <code>root</code> 会作为构造函数的一部分给出。指针应初始化为一个不存在于 BST 中的数字,且该数字小于 BST 中的任何元素。</li>
	<li><code>boolean hasNext()</code> 如果向指针右侧遍历存在数字,则返回 <code>true</code> ;否则返回 <code>false</code></li>
	<li><code>int next()</code>将指针向右移动,然后返回指针处的数字。</li>
</ul>

<p>注意,指针初始化为一个不存在于 BST 中的数字,所以对 <code>next()</code> 的首次调用将返回 BST 中的最小元素。</p>
</div>
</div>

<p>你可以假设 <code>next()</code> 调用总是有效的,也就是说,当调用 <code>next()</code> 时,BST 的中序遍历中至少存在一个下一个数字。</p>

F
fix bug  
feilong 已提交
18

每日一练社区's avatar
每日一练社区 已提交
19 20 21 22 23

<p><strong>示例:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/25/bst-tree.png" style="width: 189px; height: 178px;" />
<pre>
<strong>输入</strong>
每日一练社区's avatar
每日一练社区 已提交
24

每日一练社区's avatar
每日一练社区 已提交
25
["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"]
每日一练社区's avatar
每日一练社区 已提交
26

每日一练社区's avatar
每日一练社区 已提交
27
[[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []]
每日一练社区's avatar
每日一练社区 已提交
28

每日一练社区's avatar
每日一练社区 已提交
29
<strong>输出</strong>
每日一练社区's avatar
每日一练社区 已提交
30

每日一练社区's avatar
每日一练社区 已提交
31 32 33
[null, 3, 7, true, 9, true, 15, true, 20, false]

<strong>解释</strong>
每日一练社区's avatar
每日一练社区 已提交
34

每日一练社区's avatar
每日一练社区 已提交
35
BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]);
每日一练社区's avatar
每日一练社区 已提交
36

每日一练社区's avatar
每日一练社区 已提交
37
bSTIterator.next();    // 返回 3
每日一练社区's avatar
每日一练社区 已提交
38

每日一练社区's avatar
每日一练社区 已提交
39
bSTIterator.next();    // 返回 7
每日一练社区's avatar
每日一练社区 已提交
40

每日一练社区's avatar
每日一练社区 已提交
41
bSTIterator.hasNext(); // 返回 True
每日一练社区's avatar
每日一练社区 已提交
42

每日一练社区's avatar
每日一练社区 已提交
43
bSTIterator.next();    // 返回 9
每日一练社区's avatar
每日一练社区 已提交
44

每日一练社区's avatar
每日一练社区 已提交
45
bSTIterator.hasNext(); // 返回 True
每日一练社区's avatar
每日一练社区 已提交
46

每日一练社区's avatar
每日一练社区 已提交
47
bSTIterator.next();    // 返回 15
每日一练社区's avatar
每日一练社区 已提交
48

每日一练社区's avatar
每日一练社区 已提交
49
bSTIterator.hasNext(); // 返回 True
每日一练社区's avatar
每日一练社区 已提交
50

每日一练社区's avatar
每日一练社区 已提交
51
bSTIterator.next();    // 返回 20
每日一练社区's avatar
每日一练社区 已提交
52

每日一练社区's avatar
每日一练社区 已提交
53 54 55
bSTIterator.hasNext(); // 返回 False
</pre>

F
fix bug  
feilong 已提交
56

每日一练社区's avatar
每日一练社区 已提交
57 58 59 60 61 62 63 64 65

<p><strong>提示:</strong></p>

<ul>
	<li>树中节点的数目在范围 <code>[1, 10<sup>5</sup>]</code></li>
	<li><code>0 <= Node.val <= 10<sup>6</sup></code></li>
	<li>最多调用 <code>10<sup>5</sup></code><code>hasNext</code><code>next</code> 操作</li>
</ul>

F
fix bug  
feilong 已提交
66

每日一练社区's avatar
每日一练社区 已提交
67 68 69 70 71 72 73

<p><strong>进阶:</strong></p>

<ul>
	<li>你可以设计一个满足下述条件的解决方案吗?<code>next()</code><code>hasNext()</code> 操作均摊时间复杂度为 <code>O(1)</code> ,并使用 <code>O(h)</code> 内存。其中 <code>h</code> 是树的高度。</li>
</ul>

每日一练社区's avatar
每日一练社区 已提交
74
<p>以下<span style="color:red">错误</span>的选项是?</p>
每日一练社区's avatar
每日一练社区 已提交
75 76

## aop
77

每日一练社区's avatar
每日一练社区 已提交
78
### before
79

每日一练社区's avatar
每日一练社区 已提交
80
```c
每日一练社区's avatar
每日一练社区 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93
#include <bits/stdc++.h>
using namespace std;

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
```
每日一练社区's avatar
每日一练社区 已提交
94

每日一练社区's avatar
每日一练社区 已提交
95
### after
96

每日一练社区's avatar
每日一练社区 已提交
97
```c
每日一练社区's avatar
每日一练社区 已提交
98 99 100 101 102 103 104 105 106
/**
 * Your BSTIterator object will be instantiated and called as such:
 * BSTIterator* obj = new BSTIterator(root);
 * int param_1 = obj->next();
 * bool param_2 = obj->hasNext();
 */
```

## 答案
107

每日一练社区's avatar
每日一练社区 已提交
108
```c
每日一练社区's avatar
每日一练社区 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
class BSTIterator
{
private:
    void traverse(TreeNode *root, vector<int> &res)
    {
        if (root == nullptr)
        {
            return;
        }
        traverse(root->right, res);
        traverse(root->left, res);
        res.push_back(root->val);
        }

    vector<int> inorder(TreeNode *root)
    {
        vector<int> res;
        traverse(root, res);
        return res;
    }

    vector<int> arr;
    int idx;

public:
    BSTIterator(TreeNode *root) : idx(0), arr(inorder(root))
    {
    }

    int next()
    {
        return arr[idx++];
    }

    bool hasNext()
    {
        return idx < arr.size();
    }
};
```
## 选项

### A
每日一练社区's avatar
每日一练社区 已提交
152

每日一练社区's avatar
每日一练社区 已提交
153
```c
每日一练社区's avatar
每日一练社区 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
class BSTIterator
{
private:
    void inorder(TreeNode *root, vector<int> &vec)
    {
        if (!root)
            return;
        inorder(root->left, vec);
        vec.emplace_back(root->val);
        inorder(root->right, vec);
    }
    vector<int> inorderTraversal(TreeNode *root)
    {
        vector<int> ret;
        inorder(root, ret);
        return ret;
    }

    vector<int> buf;
    size_t idx;

public:
    BSTIterator(TreeNode *root)
    {
        idx = 0;
        buf = inorderTraversal(root);
    }

    int next()
    {
        return buf[idx++];
    }

    bool hasNext()
    {
        return idx < buf.size();
    }
};
```

### B
每日一练社区's avatar
每日一练社区 已提交
195

每日一练社区's avatar
每日一练社区 已提交
196
```c
每日一练社区's avatar
每日一练社区 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
class BSTIterator
{
public:
    stack<TreeNode *> treeMin;
    BSTIterator(TreeNode *root)
    {
        TreeNode *t = root;
        while (t)
        {
            treeMin.push(t);
            t = t->left;
        }
    }

    /** @return the next smallest number */
    int next()
    {
        TreeNode *tmp = treeMin.top();
        int res = tmp->val;
        treeMin.pop();
        tmp = tmp->right;
        while (tmp)
        {
            treeMin.push(tmp);
            tmp = tmp->left;
        }
        return res;
    }

    /** @return whether we have a next smallest number */
    bool hasNext()
    {
        if (treeMin.empty())
            return false;
        else
            return true;
    }
};
```

### C
每日一练社区's avatar
每日一练社区 已提交
238

每日一练社区's avatar
每日一练社区 已提交
239
```c
每日一练社区's avatar
每日一练社区 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
class BSTIterator
{
public:
    queue<int> q;
    BSTIterator(TreeNode *root)
    {

        inorder(root, q);
    }

    void inorder(TreeNode *root, queue<int> &q)
    {
        if (root == NULL)
            return;
        inorder(root->left, q);
        q.push(root->val);
        inorder(root->right, q);
    }

    /** @return the next smallest number */
    int next()
    {
        int tmp = q.front();
        q.pop();
        return tmp;
    }

    /** @return whether we have a next smallest number */
    bool hasNext()
    {
        if (q.empty())
            return false;
        else
            return true;
    }
};
```