Skip to content

Latest commit

 

History

History
executable file
·
240 lines (216 loc) · 5.59 KB

173. Binary Search Tree Iterator.md

File metadata and controls

executable file
·
240 lines (216 loc) · 5.59 KB

173. Binary Search Tree Iterator

Question

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.

Thinking:

  • Method:
    • 使用LinkedList装载中序遍历的结果。O(N) extra space
/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

public class BSTIterator {
    private LinkedList<Integer> list;
    private ListIterator<Integer> cur = null;
    public BSTIterator(TreeNode root) {
        list = new LinkedList<>();
        inorder(root, list);
        cur = list.listIterator(0);
    }
    private void inorder(TreeNode node, List<Integer> result){
        if(node == null) return;
        inorder(node.left, result);
        result.add(node.val);
        inorder(node.right, result);
    }
    /** @return whether we have a next smallest number */
    public boolean hasNext() {
        return cur.hasNext();
    }

    /** @return the next smallest number */
    public int next() {
        return cur.next();
    }
}

/**
 * Your BSTIterator will be called like this:
 * BSTIterator i = new BSTIterator(root);
 * while (i.hasNext()) v[f()] = i.next();
 */
  • Method 2: 通过栈实现
/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

public class BSTIterator {
    Stack<TreeNode> stack;
    public BSTIterator(TreeNode root) {
        stack = new Stack<>();
        while(root != null){
            stack.push(root);
            root = root.left;
        }
    }

    /** @return whether we have a next smallest number */
    public boolean hasNext() {
        return !stack.isEmpty();
    }

    /** @return the next smallest number */
    public int next() {
        TreeNode cur = stack.pop();
        if(cur.right != null){
            TreeNode next = cur.right;
            while(next != null){
                stack.push(next);
                next = next.left;
            }
        }
        return cur.val;
    }
}

/**
 * Your BSTIterator will be called like this:
 * BSTIterator i = new BSTIterator(root);
 * while (i.hasNext()) v[f()] = i.next();
 */

二刷

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class BSTIterator {
    private List<Integer> inorder = new LinkedList<>();
    private ListIterator<Integer> cur = null;
    public BSTIterator(TreeNode root) {
        inorder(root, inorder);
        cur = inorder.listIterator(0);
    }
    private void inorder(TreeNode root, List<Integer> list){
        if(root == null) return;
        inorder(root.left, list);
        list.add(root.val);
        inorder(root.right, list);
    }
    /** @return whether we have a next smallest number */
    public boolean hasNext() {
        return cur.hasNext();
    }
    /** @return the next smallest number */
    public int next() {
        return cur.next();
    }
}
/**
 * Your BSTIterator will be called like this:
 * BSTIterator i = new BSTIterator(root);
 * while (i.hasNext()) v[f()] = i.next();
 */

Third Time

  • Method 1: Recursion

     /**
      * Definition for a binary tree node.
      * public class TreeNode {
      *     int val;
      *     TreeNode left;
      *     TreeNode right;
      *     TreeNode(int x) { val = x; }
      * }
      */
     class BSTIterator {
         private Queue<Integer> q;
         public BSTIterator(TreeNode root) {
             q = new LinkedList<>();
             inorder(root);
         }
         private void inorder(TreeNode node){
             if(node == null) return;
             inorder(node.left);
             q.offer(node.val);
             inorder(node.right);
         }
    
         /** @return the next smallest number */
         public int next() {
             return q.poll();
         }
    
         /** @return whether we have a next smallest number */
         public boolean hasNext() {
             return !q.isEmpty();
         }
     }
    
     /**
      * Your BSTIterator object will be instantiated and called as such:
      * BSTIterator obj = new BSTIterator(root);
      * int param_1 = obj.next();
      * boolean param_2 = obj.hasNext();
      */
  • Method 2: loop

     /**
      * Definition for a binary tree node.
      * public class TreeNode {
      *     int val;
      *     TreeNode left;
      *     TreeNode right;
      *     TreeNode(int x) { val = x; }
      * }
      */
     class BSTIterator {
         Stack<TreeNode> stack;
         public BSTIterator(TreeNode root) {
             this.stack = new Stack<>();
             if(root == null) return;
             stack.push(root);
             while(root.left != null){
                 stack.push(root.left);
                 root = root.left;
             }
         }
    
         /** @return the next smallest number */
         public int next() {
             TreeNode node = stack.pop();
             if(node.right != null){
                 TreeNode temp = node.right;
                 stack.push(temp);
                 while(temp.left != null){
                     stack.push(temp.left);
                     temp = temp.left;
                 }
             }
             return node.val;
         }
    
         /** @return whether we have a next smallest number */
         public boolean hasNext() {
             return !stack.isEmpty();
         }
     }
    
     /**
      * Your BSTIterator object will be instantiated and called as such:
      * BSTIterator obj = new BSTIterator(root);
      * int param_1 = obj.next();
      * boolean param_2 = obj.hasNext();
      */