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  */
  • 相关阅读:
    LR杂记-nmon+analyser监控linux系统资源
    accept函数
    android performClick使用
    #line 的作用是改变当前行数和文件名称
    C++常用排序法、随机数
    C语言运算符优先级及结合性
    如何高效把一字节的位对换, bit0和bit7,bit1和bit6,以此类推.
    NumPy
    Rational Rose、PowerDesign、Visio的一些比较
    vld,Bounds Checker,memwatch,mtrace,valgrind,debug_new几种内存泄露检测工具的比较,Valgrind Cheatsheet
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/4244951.html
Copyright © 2011-2022 走看看