forked from algorithm008-class01/algorithm008-class01
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
54 lines (43 loc) · 1.24 KB
/
Solution.java
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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
helper(root, res);
return res;
}
private void helper(TreeNode root, List<Integer> res) {
if (root != null) {
if (root.left != null) {
helper(root.left, res);
}
res.add(root.val);
if (root.right != null) {
helper(root.right, res);
}
}
}
public static void main(String[] args) {
Solution lib = new Solution();
TreeNode root = new TreeNode(1);
root.right = new TreeNode(2);
root.right.left = new TreeNode(3);
List<Integer> result = lib.inorderTraversal(root);
List<Integer> expect = new ArrayList<Integer>();
expect.add(1);
expect.add(3);
expect.add(2);
System.out.println(result.toString());
System.out.println(expect.toString());
Arrays.equals(result.toArray(), expect.toArray());
}
}
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}