binary search tree iterator
Topic description: Implement a binary search tree iterator class BSTIterator, representing an iterator that traverses a binary search tree (BST) in inorder:
- BSTIterator(TreeNode root) Initializes an object of class BSTIterator. The root node root of the BST is given as part of the constructor. The pointer shall be initialized to a number not present in the BST that is less than any element in the BST.
- boolean hasNext() Returns true if there is a number traversing to the right of the pointer; otherwise returns false.
- int next() moves the pointer to the right and 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, there is at least one next number in the inorder traversal of the BST when next() is called.
For example descriptions, please refer to the official website of LeetCode.
Source: LeetCode
Link: https://leetcode-cn.com/problems/binary-search-tree-iterator/
The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization, and for non-commercial reprints, please indicate the source.
Solution 1: In-order traversal
First, declare a List as values in BSTIterator to store the node value of the binary tree, and initialize the node value into values in the construction method through in-order traversal.
Then, declare an index to identify the current position,
next()
method andhasNext()
The realization of the method depends on index and values for judgment.
import com.kaesar.leetcode.TreeNode;
import java.util.ArrayList;
import java.util.List;
public class LeetCode_173 {
public static void main(String[] args) {
// 测试用例
TreeNode root = new TreeNode(7);
root.left = new TreeNode(3);
root.right = new TreeNode(15);
root.right.left = new TreeNode(9);
root.right.right = new TreeNode(20);
BSTIterator bSTIterator = new BSTIterator(root);
System.out.println(bSTIterator.next()); // 返回 3
System.out.println(bSTIterator.next()); // 返回 7
System.out.println(bSTIterator.hasNext()); // 返回 True
System.out.println(bSTIterator.next()); // 返回 9
System.out.println(bSTIterator.hasNext()); // 返回 True
System.out.println(bSTIterator.next()); // 返回 15
System.out.println(bSTIterator.hasNext()); // 返回 True
System.out.println(bSTIterator.next()); // 返回 20
System.out.println(bSTIterator.hasNext()); // 返回 False
}
}
class BSTIterator {
private int index;
private List<Integer> values;
public BSTIterator(TreeNode root) {
this.index = 0;
this.values = new ArrayList<>();
inOrder(root, values);
}
/**
* 中序遍历
*
* @param root
* @param values
*/
private void inOrder(TreeNode root, List<Integer> values) {
if (root == null) {
return;
}
inOrder(root.left, values);
values.add(root.val);
inOrder(root.right, values);
}
public int next() {
return values.get(index++);
}
public boolean hasNext() {
return index < values.size();
}
}
[Daily Message] The mainstay, turning the tide of the tide, with genius, establishing a great cause, saving the people at the table, laying the country's fortune like a rock, not a hero can't handle it.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。