题目链接:LeetCode 173. 二叉搜索树迭代器

一、题解

题目大意:

实现一个中序遍历的二叉树!但是要通过迭代器实现!即完成下面三个方法!

思路:

其实就是二叉树中序遍历非递归写法的拆分!

二叉树非递归中序遍历写法:LeetCode的94题就是!

  1. 构造器处理第一部分
  2. next处理下一部分,然后还得处理和构造器相同的部分
  3. hasNext则可以直接判断栈是否为空即可

时间复杂度: O(n)

空间复杂度: O(h),h为树的高度

二、AC代码

参考代码:

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
/**
* Definition for a binary tree node.
* 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) {}
* };
*/
class BSTIterator {
public:
stack<TreeNode*> stk;
BSTIterator(TreeNode* root) {
while(root){
stk.push(root);
root = root->left;
}
}

int next() {
auto root = stk.top();
stk.pop();
int val = root->val;
root = root->right;
while(root){
stk.push(root);
root = root->left;
}
return val;
}

bool hasNext() {
return stk.size();
}
};

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