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()时,在把目前节点的右子树的左排节点依次压入

    好记性不如烂笔头,内存虽快,但不持久
  • 相关阅读:
    PHP中的闭包详解
    PHPDoc 学习记录
    php中注释有关内容
    命名空间 转
    php命名空间学习笔记。
    php命名空间详解
    命名空间
    php作用域限定符
    nginx负载均衡
    naginx安装入门
  • 原文地址:https://www.cnblogs.com/onlysun/p/4419388.html
Copyright © 2011-2022 走看看