zoukankan      html  css  js  c++  java
  • LeetCode

    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  */
  • 相关阅读:
    MySql插入查询的数据(命名Sql常用)
    odoo前端
    巧用 Odoo act_window 的 flags实现一些个性化的视图控制
    JDK安装
    2018年3月开始新的一年计划
    odoo10 api 装饰器
    odoo10 添加自动执行server action
    odoo中各组件的颜色及用法tree,kanban,many2many_tags,app_ui_enhance
    python virtualenv学习
    odoo10 fields.Selection 根据权限显示不同的selection内容
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/4244951.html
Copyright © 2011-2022 走看看