[Java]leetcode173 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.

题意:写一个二叉查找树的迭代器,实现hasNext()和next()的功能。next()每次返回二叉树中未访问的最小值。要求的平均时间复杂度是O(1)和空间复杂度是O(h)。

解题思路:next()每次返回二叉树中未访问的最小值。也就是将二叉树中序遍历,并将对应值输出。这里面注意的是空间和时间的复杂度。

public class BSTIterator {//将中序遍历的功能嵌查在整个程序中
    TreeNode current;
    Stack<TreeNode> stack;
    public BSTIterator(TreeNode root) {
        current=root;
        stack=new Stack<TreeNode>();
        while(current!=null)//因为只可能是左节点才是最小值,将所有左子树节点入栈,保证空间复杂度是O(h)
        {
            stack.push(current);
            current=current.left;
        }
    }
    /** @return whether we have a next smallest number */
    public boolean hasNext() {
     return !stack.isEmpty();
    }
    /** @return the next smallest number */
    public int next() {
      current=stack.pop();
      int res=current.val;//这一步已经得到最小值
      current=current.right;//但要考虑到右子树的遍历
      while(current!=null)
       {
         stack.push(current);
         current=current.left;
       }
       return res;
      }
}
时间: 2024-10-09 20:00:29

[Java]leetcode173 Binary Search Tree Iterator的相关文章

LeetCode173——Binary Search Tree Iterator

终于进入了中等难度的题目了.貌似leetcode不停地加题量,要做完正的是不那么容易啊.新年开工,小伙伴们都在晒红包,深圳这边 上班第一天一般都有发红包,不知道其他地方的是不是也这样. 到目前为止,做了40题了,不得不感慨,算法有待提高.大学真是白过了,人生那么美好的四年白过了,这是多么的悲哀. 现在想想,一个人要想在大学过得有意义,对以后的人生打下坚实的基础,那么最迟在大学一年之后,一定要有较广的见识,对社会有充分 的认识.总之,足够的广阔视野,才能让你知道当前的路怎么走.现实呢,大学专业往往

[LeetCode] Binary Search Tree Iterator

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

LeetCode: Binary Search Tree Iterator 解题报告

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

[LintCode] Binary Search Tree Iterator

Binary Search Tree Iterator Design an iterator over a binary search tree with the following rules: Elements are visited in ascending order (i.e. an in-order traversal) next() and hasNext() queries run in O(1) time inaverage. Example For the following

【LeetCode】Binary Search Tree Iterator (2 solutions)

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

[LeetCode] 173. Binary Search Tree Iterator Java

题目: 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 u

? leetcode 173. Binary Search Tree Iterator 设计迭代器(搜索树)--------- java

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

Java for 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 return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1) time and uses

[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. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1