zoukankan      html  css  js  c++  java
  • Binary Search Tree Iterator

    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.

    问题描述:BST的Iterator,next()从小到大依次给出,next(),hasNext() O(1)时间复杂度 ,O(h)内存高度;

    1:初始化时一次性中序遍历到arraylist中(体现不出O(h)要求):

    2:初始化把树的最左排的节点依次压入stack中,hasNext() stack.isEmpty(),当调用next()时,在把目前节点的右子树的左排节点依次压入

    好记性不如烂笔头,内存虽快,但不持久
  • 相关阅读:
    一个基于Angular+Ionic+Phonegap的混合APP实战
    Kafka
    kafka
    Kafka
    归并排序
    插入排序
    CC1310的晶振问题
    CC1310的RSSI值问题
    CC1310之使用SMARTRF STUDIO
    CC1310电源
  • 原文地址:https://www.cnblogs.com/onlysun/p/4419388.html
Copyright © 2011-2022 走看看