File tree Expand file tree Collapse file tree 3 files changed +114
-0
lines changed
solutions/173.Binary_Search_Tree_Iterator Expand file tree Collapse file tree 3 files changed +114
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * Author: illuz <iilluzen[at]gmail.com>
3
+ * File: AC_pre_inorder_n.cpp
4
+ * Create Date: 2015-03-16 10:07:47
5
+ * Descripton:
6
+ */
7
+
8
+ #include < bits/stdc++.h>
9
+
10
+ using namespace std ;
11
+ const int N = 0 ;
12
+
13
+ // Definition for binary tree
14
+ struct TreeNode {
15
+ int val;
16
+ TreeNode *left;
17
+ TreeNode *right;
18
+ TreeNode (int x) : val(x), left(NULL ), right(NULL ) {}
19
+ };
20
+
21
+ class BSTIterator {
22
+ private:
23
+ queue<int > in_res;
24
+ void inorder (TreeNode *root) {
25
+ if (root != NULL ) {
26
+ inorder (root->left );
27
+ in_res.push (root->val );
28
+ inorder (root->right );
29
+ }
30
+ }
31
+
32
+ public:
33
+ BSTIterator (TreeNode *root) {
34
+ // generate the inorder list
35
+ while (!in_res.empty ())
36
+ in_res.pop ();
37
+ inorder (root);
38
+ }
39
+
40
+ /* * @return whether we have a next smallest number */
41
+ bool hasNext () {
42
+ return in_res.size () != 0 ;
43
+ }
44
+
45
+ /* * @return the next smallest number */
46
+ int next () {
47
+ int front = in_res.front ();
48
+ in_res.pop ();
49
+ return front;
50
+ }
51
+ };
52
+
53
+ int main () {
54
+ BSTIterator i = BSTIterator (root);
55
+ while (i.hasNext ()) cout << i.next ();
56
+ return 0 ;
57
+ }
58
+
59
+
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/python
2
+ # -*- coding: utf-8 -*-
3
+ # Author: illuz <iilluzen[at]gmail.com>
4
+ # File: AC_stack_n.py
5
+ # Create Date: 2015-03-16 10:19:51
6
+ # Usage: AC_stack_n.py
7
+ # Descripton:
8
+
9
+
10
+ # Definition for a binary tree node
11
+ class TreeNode :
12
+ def __init__ (self , x ):
13
+ self .val = x
14
+ self .left = None
15
+ self .right = None
16
+
17
+ class BSTIterator :
18
+ # @param root, a binary search tree's root node
19
+ def __init__ (self , root ):
20
+ self .stack = []
21
+ while root :
22
+ self .stack .append (root )
23
+ root = root .left
24
+
25
+ # @return a boolean, whether we have a next smallest number
26
+ def hasNext (self ):
27
+ return len (self .stack ) != 0
28
+
29
+ # @return an integer, the next smallest number
30
+ def next (self ):
31
+ top = self .stack .pop ()
32
+ right = top .right
33
+ while right :
34
+ self .stack .append (right )
35
+ right = right .left
36
+ return top .val
37
+
38
+ # debug
39
+ root = TreeNode (0 )
40
+ i , v = BSTIterator (root ), []
41
+ while i .hasNext (): v .append (i .next ())
Original file line number Diff line number Diff line change
1
+ ## 173. Binary Search Tree Iterator (Medium)
2
+
3
+ ### ** 链接** :
4
+ 题目:https://leetcode.com/problems/binary-search-tree-iterator/
5
+ 代码(github):https://github.com/illuz/leetcode
6
+
7
+ ### ** 题意** :
8
+ 实现一个 BST 的类。
9
+
10
+ ### ** 分析** :
11
+
12
+ 1 . ** (C++)** 初始化的时候就将中序遍历处理好,保存在 queue 里
13
+ 2 . ** (Python)** 用 stack 做,一边求一边处理即可。
14
+ 3 . Morris Travese Tree,可以用 O(1) 的空间解决这题,详见 [ 099. Recover Binary Search Tree (Hard)] ( https://github.com/illuz/leetcode/blob/master/solutions/099.Recover_Binary_Search_Tree )
You can’t perform that action at this time.
0 commit comments