leetcode 173. Binary Search Tree Iterator

Posted hwd9654

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode 173. Binary Search Tree Iterator相关的知识,希望对你有一定的参考价值。

技术图片

 

class BSTIterator 
    private Stack<TreeNode> stack;
    public BSTIterator(TreeNode root) 
        stack = new Stack<>();
        while(root != null)
            stack.push(root);
            root = root.left;
        
    
    
    /** @return the next smallest number */
    public int next() 
        TreeNode cur = stack.pop();
        TreeNode tmp = cur;
        if(tmp != null)
            tmp = tmp.right;
            while(tmp != null)
                stack.push(tmp);
                tmp = tmp.left;
            
        
        
        return cur.val;
    
    
    /** @return whether we have a next smallest number */
    public boolean hasNext() 
        
        return !stack.isEmpty();
    

 

以上是关于leetcode 173. Binary Search Tree Iterator的主要内容,如果未能解决你的问题,请参考以下文章

leetcode 173. Binary Search Tree Iterator

[LeetCode] 173. Binary Search Tree Iterator Java

[LeetCode] 173. Binary Search Tree Iterator_Medium_tag: Binary Search Tree

Leetcode solution 173: Binary Search Tree Iterator

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

LeetCode 173. Binary Search Tree Iterator(搜索二叉树)