2022-08-04 23:11:25 +02:00
|
|
|
import java.util.ArrayDeque;
|
|
|
|
|
|
|
|
/* *
|
|
|
|
* 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;
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
|
|
|
|
class TreeNode {
|
2024-03-02 21:01:53 +01:00
|
|
|
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;
|
|
|
|
}
|
2022-08-04 23:11:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class BSTIterator {
|
2024-03-02 21:01:53 +01:00
|
|
|
private enum Position {
|
|
|
|
Todo,
|
|
|
|
Done
|
|
|
|
}
|
2022-08-04 23:11:25 +02:00
|
|
|
|
2024-03-02 21:01:53 +01:00
|
|
|
private class Entry {
|
|
|
|
public TreeNode node;
|
|
|
|
public Position position;
|
2022-08-04 23:11:25 +02:00
|
|
|
|
2024-03-02 21:01:53 +01:00
|
|
|
public Entry(TreeNode node, Position position) {
|
|
|
|
this.node = node;
|
|
|
|
this.position = position;
|
|
|
|
}
|
2022-08-04 23:11:25 +02:00
|
|
|
|
2024-03-02 21:01:53 +01:00
|
|
|
public void expand(ArrayDeque<Entry> stack) {
|
|
|
|
if (node.right != null) {
|
|
|
|
stack.addLast(new Entry(node.right, Position.Todo));
|
|
|
|
}
|
2022-08-04 23:11:25 +02:00
|
|
|
|
2024-03-02 21:01:53 +01:00
|
|
|
this.position = Position.Done;
|
|
|
|
stack.addLast(this);
|
2022-08-04 23:11:25 +02:00
|
|
|
|
2024-03-02 21:01:53 +01:00
|
|
|
if (node.left != null) {
|
|
|
|
stack.addLast(new Entry(node.left, Position.Todo));
|
|
|
|
}
|
2022-08-04 23:11:25 +02:00
|
|
|
}
|
2024-03-02 21:01:53 +01:00
|
|
|
}
|
2022-08-04 23:11:25 +02:00
|
|
|
|
2024-03-02 21:01:53 +01:00
|
|
|
private ArrayDeque<Entry> stack = new ArrayDeque<>();
|
2022-08-04 23:11:25 +02:00
|
|
|
|
2024-03-02 21:01:53 +01:00
|
|
|
public BSTIterator(TreeNode root) {
|
|
|
|
stack.addLast(new Entry(root, Position.Todo));
|
|
|
|
}
|
2022-08-04 23:11:25 +02:00
|
|
|
|
2024-03-02 21:01:53 +01:00
|
|
|
public int next() {
|
|
|
|
for (Entry last = stack.getLast();
|
|
|
|
last != null && last.position == Position.Todo;
|
|
|
|
last = stack.getLast()) {
|
|
|
|
stack.removeLast().expand(stack);
|
2022-08-04 23:11:25 +02:00
|
|
|
}
|
|
|
|
|
2024-03-02 21:01:53 +01:00
|
|
|
return stack.removeLast().node.val;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasNext() {
|
|
|
|
Entry last = stack.peekLast();
|
|
|
|
return last != null && (last.position != Position.Todo || last.node != null);
|
|
|
|
}
|
2022-08-04 23:11:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-03-02 21:01:53 +01:00
|
|
|
* 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();
|
2022-08-04 23:11:25 +02:00
|
|
|
*/
|