BSTIterator-Binary Search Tree Iterator

Title

Implement a binary search tree iterator class BSTIterator, which represents an iterator that traverses the binary search tree (BST) in order:

BSTIterator(TreeNode root) initializes an object of the BSTIterator class. The root node root of the BST will be given as part of the constructor
. The pointer should be initialized to a number that does not exist in the BST, and the number is less than any element in the BST.
boolean hasNext() Returns true if there are numbers traversing to the right of the pointer; otherwise, returns false.
int next() moves the pointer to the right, and then returns the number at the pointer.

Note that the pointer is initialized to a number that does not exist in the BST, so the first call to next() will return the smallest element in the BST.

You can assume that the next() call is always valid, that is, when next() is called, there is at least one next number in the in-order traversal of the BST.

Example:

输入
[“BSTIterator”, “next”, “next”, “hasNext”, “next”, “hasNext”, “next”, "hasNext
", “next”, “hasNext”]
[[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []]
输出
[null, 3, 7, true, 9, true, 15, true, 20, false]

Interpret
BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]);
bSTIterator.next(); // return 3
bSTIterator.next(); // return 7
bSTIterator.hasNext(); // return True
bSTIterator.next(); // return 9
bSTIterator.hasNext(); // return True
bSTIterator.next(); // return 15
bSTIterator.hasNext(); // return True
bSTIterator.next(); // return 20
bSTIterator.hasNext(); // return False

prompt:

The number of nodes in the tree is within the range [1, 105]
0 <= Node.val <= 106
Call hasNext and next operations up to 105 times

Advanced:

Can you design a solution that meets the following conditions? The next() and hasNext() operations share the time complexity of O(1) and use O(h) memory. Where h is the height
of the tree .

Supplementary conditions


  Definition for a binary tree node.
  public class TreeNode {
    
    
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {
    
    }
      TreeNode(int val) {
    
     this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
    
    
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }

Problem-solving ideas

Traverse in middle order, put the nodes of the tree into a list

Code demo

class BSTIterator {
    
    

    List<Integer> list=new ArrayList<>();
    //定义两个变量,count作为一个指针在list中遍历,num存放list的大小
    int count=0;
    int num=0;
    public BSTIterator(TreeNode root) {
    
    
        digui(root);

    }
    //用递归左右子树按照中序遍历入树
    public void digui(TreeNode root)
    {
    
    
        if(root.left!=null)
        {
    
    
            digui(root.left);
            list.add(root.val);
        }
        else
        {
    
    
            list.add(root.val);
        }
        if(root.right!=null)
            digui(root.right);
        num=list.size();
    }
    
    public int next() {
    
    
        return hasNext() ? list.get(count++) : null;
    }
    
    public boolean hasNext() {
    
    
       return count<=num-1 ;
    }
}

effect

The info
answer was successful:
execution time: 22 ms, defeating 95.60% of Java users
Memory consumption: 41.5 MB, defeating 99.75% of Java users

Guess you like

Origin blog.csdn.net/tangshuai96/article/details/115276460