关闭

LeetCode:Binary Search Tree Iterator

标签: LeetCodeBinary Search Tree I
545人阅读 评论(0) 收藏 举报
分类:

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();
 */


0
0
查看评论

leetcode-173:Binary Search Tree Iterator(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....
  • wen1158646729
  • wen1158646729
  • 2015-08-18 10:58
  • 1128

[LeetCode 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...
  • sbitswc
  • sbitswc
  • 2015-08-21 14:11
  • 596

Leetcode: 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...
  • doc_sgl
  • doc_sgl
  • 2015-01-03 22:13
  • 3630

leetcode 173: Binary Search Tree Iterator

Binary Search Tree Iterator Total Accepted: 1141 Total Submissions: 3857 Implement an iterator over a binary search tree (BST). Your iterator ...
  • xudli
  • xudli
  • 2015-01-02 12:44
  • 4102

Validate Binary Search Tree -- LeetCode

原题链接: http://oj.leetcode.com/problems/validate-binary-search-tree/  这道题是检查一颗二分查找树是否合法,二分查找树是非常常见的一种数据结构,因为它可以在O(logn)时间内实现搜索。这里我们介绍两种方法来解决这...
  • linhuanmars
  • linhuanmars
  • 2014-04-16 04:34
  • 11690

Leetcode 98. Validate Binary Search Tree C++及javascript解答,不同语言不同玩法

题意:给定一棵二叉树,写程序判断这颗二叉树是否为合法的二分查找树(对于节点root,其所有左子树中节点都满足 node.val root.val) 转载请注明出处:http://blog.csdn.net/sunny606 C++: /** * Definition for a binar...
  • sunny606
  • sunny606
  • 2017-03-15 22:03
  • 215

leetcode 270: Closest Binary Search Tree Value

Closest Binary Search Tree Value Total Accepted: 1815 Total Submissions: 6418 Difficulty: Easy Given a non-empty binary search tree ...
  • xudli
  • xudli
  • 2015-09-26 09:43
  • 5229

[LeetCode] 501. Find Mode in Binary Search Tree 解题报告

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a B...
  • a921122
  • a921122
  • 2017-04-28 05:41
  • 1211

【LeetCode】669 Trim a Binary Search Tree

原题地址:669. Trim a Binary Search Tree。题意给定一棵二叉搜索树和最小最大边界L和R,修剪这棵树,使得其中的元素位于L和R之间。解法 当root的值位于L和R之间,则递归修剪其左右子树,返回root。 当root的值小于L,则其左子树的值都小于L,抛弃左子树,返回修剪过...
  • hlljjy123
  • hlljjy123
  • 2017-09-08 15:53
  • 1224

[LeetCode]Binary Search Tree Iterator,解题报告

题目     LeetCode题目如下: mplement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. C...
  • zinss26914
  • zinss26914
  • 2015-02-09 21:11
  • 1399
    个人资料
    • 访问:390578次
    • 积分:6794
    • 等级:
    • 排名:第4134名
    • 原创:321篇
    • 转载:1篇
    • 译文:0篇
    • 评论:34条
    博客专栏
    最新评论