LeetCode[18] - Binary Search Tree Iterator

李特這的這個題目不錯。寫一遍example就能看出來inorder traversal。當然啦,不能直接全部traverse了,因為題目說有空間限制。

那么就traversal on the fly, 先左手DFS, 然后每次加上一個右手node,都再來一遍左手DFS。

存到一個后進先出的數據結構里,stack唄,然后頭頂就是最小的了。

/*
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.

Tags: Tree Stack Design
Similar Problems: (M) Binary Tree Inorder Traversal, (M) Flatten 2D Vector, (M) Zigzag Iterator, (M) Peeking Iterator, (M) Inorder Successor in BST

*/

/*
Attempt, Thoughts:
Test
                5
        3               9
    1       4       6       10
return: 1,3,4,5,6,9,10. Looks like in-order taversal style, though don't traversal all at once because we can only store O(h) elements.
However, we can do inorder traversal on the fly, by mantaining a stack.
How about Priority queue of all left-most elements. 
    Do a run-through on left elements, add them all.
    When pop one element:
        (it cannot have left, because we've initially added them already)
        if has right:
            add right node
            check right's node's left-most(DFS), added all left nodes and left nodes' left-child
Well.. the way I did it, does not need priority queue. Just use a stack will be fine.
*/
/**
 * 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<TreeNode>();
    public BSTIterator(TreeNode root) {
        if (root == null) {
            return;
        }
        stack.push(root);
        while(root.left != null) {
            root = root.left;
            stack.push(root);
        }
    }

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

    /** @return the next smallest number */
    public int next() {
        TreeNode node = stack.pop();
        int rst = node.val;
        if (node.right != null) {
            node = node.right;
            stack.push(node);
            while(node.left != null) {
                node = node.left;
                stack.push(node);
            }
        }
        return rst;
    }
}

/**
 * Your BSTIterator will be called like this:
 * BSTIterator i = new BSTIterator(root);
 * while (i.hasNext()) v[f()] = i.next();
 */
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 背景 一年多以前我在知乎上答了有關LeetCode的問題, 分享了一些自己做題目的經驗。 張土汪:刷leetcod...
    土汪閱讀 12,771評論 0 33
  • Validate Binary Search Tree Method: Traverse and Divide a...
    TQINJS閱讀 612評論 0 0
  • 94. Binary Tree Inorder Traversal 中序 inorder:左節點->根節點->右節...
    Morphiaaa閱讀 587評論 0 0
  • LeetCode 刷題隨手記 - 第一部分 前 256 題(非會員),僅算法題,的吐槽 https://leetc...
    蕾娜漢默閱讀 17,932評論 2 36
  • 總結類型: 完全子樹(#222) BST(左右子樹值的性質,注意不僅要滿足parent-child relatio...
    __小赤佬__閱讀 724評論 0 0