随笔- 509  文章- 0  评论- 151  阅读- 22万 

Binary Search Tree Iterator

2015.1.23 18:58

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.

Calling next() will return the next smallest number in the BST.

Note: next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree.

Solution:

  This is a BST, so the next smallest element is exactly the inorder successor. Make a whole inorder traversal and you'll get every successor.

  Use a hash map to record the result, so that you can retrieve them in O(1) time later.

  The traversal will be done immediately after receiving the root, that is, inside the constructor.

  Time complexity on average is O(1), space complexity is O(n).

Accepted code:

复制代码
 1 // 1CE, 1MLE, 1WA, 1AC, anonther good problem
 2 #include <unordered_map>
 3 using namespace std;
 4 /**
 5  * Definition for binary tree
 6  * struct TreeNode {
 7  *     int val;
 8  *     TreeNode *left;
 9  *     TreeNode *right;
10  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
11  * };
12  */
13 class BSTIterator {
14 public:
15     BSTIterator(TreeNode *root) {
16         nn.clear();
17         currentNode = nullptr;
18         
19         if (root == nullptr) {
20             return;
21         }
22         
23         TreeNode *ll, *rr;
24         findSuccessor(root, ll, rr);
25         currentNode = ll;
26     }
27 
28     /** @return whether we have a next smallest number */
29     bool hasNext() {
30         return currentNode != nullptr;
31     }
32 
33     /** @return the next smallest number */
34     int next() {
35         int val = currentNode->val;
36         currentNode = nn[currentNode];
37         return val;
38     }
39     
40     ~BSTIterator() {
41         nn.clear();
42     }
43 private:
44     unordered_map<TreeNode *, TreeNode *> nn;
45     TreeNode *currentNode;
46     
47     void findSuccessor(TreeNode *root, TreeNode *&left, TreeNode *&right) {
48         TreeNode *ll, *lr, *rl, *rr;
49         
50         if (root->left == nullptr) {
51             ll = root;
52             lr = root;
53         } else {
54             findSuccessor(root->left, ll, lr);
55             nn[lr] = root;
56         }
57         
58         if (root->right == nullptr) {
59             rl = root;
60             rr = root;
61         } else {
62             findSuccessor(root->right, rl, rr);
63             nn[root] = rl;
64         }
65         left = ll;
66         right = rr;
67     }
68 };
69 
70 /**
71  * Your BSTIterator will be called like this:
72  * BSTIterator i = BSTIterator(root);
73  * while (i.hasNext()) cout << i.next();
74  */
复制代码

 

 posted on   zhuli19901106  阅读(341)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示