LeetCode:Binary Search Tree Iterator

原创 2016年06月01日 16:18:51

Binary Search Tree Iterator




Total Accepted: 49694 Total Submissions: 141917 Difficulty: Medium

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.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

Hide Tags
 Tree Stack Design




















思路:

设有BST:

   4

 2   7

0 3 5 8


将root的所有“左”孩子入栈stack(4->2->0)

next()方法:

0(栈顶,下同):由于0没有右孩子,无操作,这时stack(4->2);

2:2有右孩子3,将3的“左”孩子入栈,这里stack(4->3);

3:stack(4);

4:stack(7->5)

5:stack(7)

7:stack(8)

8:stack(null)


hasNext()方法:判断stack是否为空即可;



java code:

/**
 * 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) {
        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 tmp = stack.pop();
        pushLeft(tmp.right);
        return tmp.val;
    }
    
    // 自定义函数:将“左”孩子入栈
    public 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();
 */


版权声明:本文为博主原创文章,转载请注明出处。

相关文章推荐

《leetCode》:Binary Search Tree Iterator

Binary Search Tree Iterator题目如下:Implement an iterator over a binary search tree (BST). Your iterator...

<LeetCode OJ> 173. Binary Search Tree Iterator

Total Accepted: 50775 Total Submissions: 144066 Difficulty: Medium Implement an iterator over a...

[leetcode]Binary Search Tree Iterator

Binary Search Tree Iterator   Implement an iterator over a binary search tree (BST). Your it...

[LeetCode]173.Binary Search Tree Iterator

【题目】 Implement an iterator over a binary search tree (BST). Your iterator will be initialized with...

leetcode-173:Binary Search Tree Iterator(Java)

Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterato...

LeetCode-Binary Search Tree Iterator-解题报告

原题链接https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a binary ...

LeetCode | Binary Search Tree Iterator

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro...

LeetCode Binary Search Tree Iterator

在一棵二叉搜索树上实现一个迭代器,初始化参数为该二叉搜索树的根节点。当调用迭代器的next()方法时,返回该二叉搜索树中最小的元素。...

LeetCode173-Binary Search Tree Iterator

LeetCode 173 : Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Yo...

Leetcode:Binary Search Tree Iterator

最近真是各种私事,也顺便给自己懒惰的机会了。今天开始要继续坚持! 看了这道题,顿时觉得自己太挫,Binary Search Tree居然不知道什么意思。赶紧百度搜索了下。下面这篇文章其实就挺简单易...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode:Binary Search Tree Iterator
举报原因:
原因补充:

(最多只允许输入30个字)