173. 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.

題解:最開始有點(diǎn)誤解,其實(shí)就是按照從小到大iterator這個(gè)樹读宙。類似于中序遍歷

每次彈出一個(gè)node稚补,則將node的right和right的所有左branch壓棧

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

public class BSTIterator {
    private Stack<TreeNode> stack = new Stack<>();

    public BSTIterator(TreeNode root) {
        pushAll(root);
    }

    /** @return whether we have a next smallest number */
    public boolean hasNext() {
        return !stack.isEmpty();
    }

    /** @return the next smallest number */
    public int next() {
        TreeNode tmpNode = stack.pop();
        pushAll(tmpNode.right);
        return tmpNode.val;
    }
    
    private void pushAll(TreeNode root){
        for(; root!=null; stack.push(root), root = root.left);
    }
}

/**
 * Your BSTIterator will be called like this:
 * BSTIterator i = new BSTIterator(root);
 * while (i.hasNext()) v[f()] = i.next();
 */

二刷
思路同上

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

public class BSTIterator {

    private Stack<TreeNode> stack;
    
    public BSTIterator(TreeNode root) {
        stack = new Stack<>();
        pushLeft(root);
    }

    /** @return whether we have a next smallest number */
    public boolean hasNext() {
        return !stack.isEmpty();
    }

    /** @return the next smallest number */
    public int next() {
        TreeNode cur = stack.pop();
        pushLeft(cur.right);
        return cur.val;
        
    }
    
    private void pushLeft(TreeNode root){
        while(root!=null){
            stack.push(root);
            root = root.left;
        }
    }
}

/**
 * Your BSTIterator will be called like this:
 * BSTIterator i = new BSTIterator(root);
 * while (i.hasNext()) v[f()] = i.next();
 */
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末嫁蛇,一起剝皮案震驚了整個(gè)濱河市棠众,隨后出現(xiàn)的幾起案子有决,更是在濱河造成了極大的恐慌,老刑警劉巖新荤,帶你破解...
    沈念sama閱讀 222,378評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件苛骨,死亡現(xiàn)場離奇詭異,居然都是意外死亡痒芝,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,970評論 3 399
  • 文/潘曉璐 我一進(jìn)店門澄者,熙熙樓的掌柜王于貴愁眉苦臉地迎上來粱挡,“玉大人,你說我怎么就攤上這事询筏∈郏” “怎么了?”我有些...
    開封第一講書人閱讀 168,983評論 0 362
  • 文/不壞的土叔 我叫張陵灌危,是天一觀的道長。 經(jīng)常有香客問我沫勿,道長,這世上最難降的妖魔是什么诫惭? 我笑而不...
    開封第一講書人閱讀 59,938評論 1 299
  • 正文 為了忘掉前任夕土,我火速辦了婚禮瘟判,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘拷获。我一直安慰自己,他們只是感情好赢笨,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,955評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著萧吠,像睡著了一般桐筏。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上绊袋,一...
    開封第一講書人閱讀 52,549評論 1 312
  • 那天铸鹰,我揣著相機(jī)與錄音,去河邊找鬼展姐。 笑死剖毯,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的擂达。 我是一名探鬼主播胶滋,決...
    沈念sama閱讀 41,063評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼俭令!你這毒婦竟也來了部宿?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,991評論 0 277
  • 序言:老撾萬榮一對情侶失蹤赫蛇,失蹤者是張志新(化名)和其女友劉穎棍掐,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體作煌,經(jīng)...
    沈念sama閱讀 46,522評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,604評論 3 342
  • 正文 我和宋清朗相戀三年奏寨,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了鹰服。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,742評論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡套菜,死狀恐怖设易,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情顿肺,我是刑警寧澤,帶...
    沈念sama閱讀 36,413評論 5 351
  • 正文 年R本政府宣布旷祸,位于F島的核電站讼昆,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏控淡。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,094評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望凭戴。 院中可真熱鬧,春花似錦者冤、人聲如沸档痪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,572評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽困后。三九已至摇予,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間侧戴,已是汗流浹背跌宛。 一陣腳步聲響...
    開封第一講書人閱讀 33,671評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留疆拘,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,159評論 3 378
  • 正文 我出身青樓丹锹,卻偏偏與公主長得像楣黍,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子租漂,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,747評論 2 361

推薦閱讀更多精彩內(nèi)容