leetcode题解-173-二叉搜索树迭代器

题目

windliang

队列保存了所有的节点值

java实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class BSTIterator {

Queue<Integer> queue = new LinkedList<>();

public BSTIterator(TreeNode root) {
inorderTraversal(root);
}

private void inorderTraversal(TreeNode root) {
if (root == null) {
return;
}
inorderTraversal(root.left);
queue.offer(root.val);
inorderTraversal(root.right);
}

/** @return the next smallest number */
public int next() {
return queue.poll();
}

/** @return whether we have a next smallest number */
public boolean hasNext() {
return !queue.isEmpty();
}
}
C++实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class BSTIterator {
private:
queue<int> q;

void inOrder(TreeNode* root)
{
if (!root) return;
inOrder(root->left);
q.push(root->val);
inOrder(root->right);
}

public:
BSTIterator(TreeNode* root)
{
inOrder(root);
}

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

/** @return whether we have a next smallest number */
bool hasNext()
{
return !q.empty();
}
};

/**
* 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();
*/

要控制中序遍历的进程,一个一个输出

java实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class BSTIterator {
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = null;

public BSTIterator(TreeNode root) {
cur = root;
}

/** @return the next smallest number */
public int next() {
int res = -1;
while (cur != null || !stack.isEmpty()) {
// 节点不为空一直压栈
while (cur != null) {
stack.push(cur);
cur = cur.left; // 考虑左子树
}
// 节点为空,就出栈
cur = stack.pop();
res = cur.val;
// 考虑右子树
cur = cur.right;
break;
}

return res;
}

/** @return whether we have a next smallest number */
public boolean hasNext() {
return cur != null || !stack.isEmpty();
}
}
C++实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class BSTIterator {
private:
stack<TreeNode*> s;
TreeNode* cur = nullptr;
public:
BSTIterator(TreeNode* root)
{
cur = root;
}

/** @return the next smallest number */
int next()
{
int res = -1;
while (cur || !s.empty())
{
while (cur != nullptr)
{
s.push(cur);
cur = cur->left;
}

cur = s.top(); s.pop();
res = cur->val;

cur = cur->right;
break;
}

return res;
}

/** @return whether we have a next smallest number */
bool hasNext() {
return cur || !s.empty();
}
};

/**
* 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();
*/

总结

  • 利用 二叉搜索树中序遍历升序序列 的特点,可以提前将树的结构以中序遍历结果存储起来