ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

173. Binary Search Tree Iterator

2022-02-06 12:32:55  阅读:145  来源: 互联网

标签:pre Binary Search TreeNode root head return 173 public


For this problem, if don't consider the follow up limitation, the solution is very easy:

The time complexity is O(1), the space complexity is O(n), n is the total node number of the tree.

class BSTIterator {

    List<Integer> list = new ArrayList<>();
    Iterator<Integer> it;
    public BSTIterator(TreeNode root) {
       inOrder(root);
        it = list.iterator();
    }
    private void inOrder(TreeNode root){
        if(root==null)
            return;
        inOrder(root.left);
        list.add(root.val);
        inOrder(root.right);
    }
    
    public int next() {
        return it.next();
    }
    
    public boolean hasNext() {
        return it.hasNext();
    }
}

But the problem have a follow up:

Could you implement next() and hasNext() to run in average O(1) time and use O(h) memory, where h is the height of the tree?

I can think of a solution with next() and hasNext() to run in average O(1) time, and use O(1) memory, but this solution need to change the tree's structure. This solution conver the tree to a double direction linked list, inspired by: 426. Convert Binary Search Tree to Sorted Doubly Linked List

class BSTIterator {

    private TreeNode head,  pre = new TreeNode();
    public BSTIterator(TreeNode root) {
        head = root;
        if(head!=null){
            while(head.left!=null)
                 head = head.left;   //1. find the smallest element
        inOrder(root);
        pre.right = head;
        head.left = pre;
        pre.right=head;
      }
    }
        
    private void inOrder(TreeNode root){ 
        if(root == null)
            return;
        inOrder(root.left);
        pre.right = root;
        root.left = pre;
        pre = root;
        inOrder(root.right);
    }
    int count =0;
    public int next() {
        count++;
        pre=pre.right;
        return pre.val;
    }
    
    public boolean hasNext() {
        return count>0 && pre.right!=head||count==0;
    }
}

The following is other's solution, the space complexity is O(h), but either next() or hasNext() cannot have O(1) time complexity:

class BSTIterator {
    private Stack<TreeNode> stk = new Stack<>();

    public BSTIterator(TreeNode root) {
        pushLeft(root);
    }

    private pushLeft(TreeNode root) {
        while (root != null) {
            stk.push(root);
            root = root.left;
        }
    }

    public int next() {
        TreeNode temp = stk.pop();
        pushLeft(temp.right)
        return temp.val;
    }

    public boolean hasNext() {
        return !stk.isEmpty();
    }
}

 

标签:pre,Binary,Search,TreeNode,root,head,return,173,public
来源: https://www.cnblogs.com/feiflytech/p/15865485.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有