Repository navigation
Expand file tree
/
Copy pathSolution.java
More file actions
77 lines (65 loc) · 1.49 KB
/
Copy pathSolution.java
File metadata and controls
77 lines (65 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.yeahqing.medium._173;
import java.util.ArrayList;
import java.util.List;
/**
* @author YeahQing
* @date 2022/10/21
* 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;
* }
* }
*/
public class Solution {
public static void main(String[] args) {
// BSTIterator obj = new BSTIterator(root);
// int param_1 = obj.next();
// boolean param_2 = obj.hasNext();
}
}
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 BSTIterator {
private final List<Integer> arr;
private int index;
public BSTIterator(TreeNode root) {
index = 0;
// 中序遍历
arr = new ArrayList<Integer>();
// 使用数组保存中序遍历的结果
inorder(root);
}
private void inorder(TreeNode root) {
if (root == null) return;
inorder(root.left);
arr.add(root.val);
inorder(root.right);
}
public int next() {
return arr.get(index++);
}
public boolean hasNext() {
return index < arr.size();
}
}