import java.util.*;
public class Main {
public int[] solution(TreeNode<Integer> root) {
//因为事先无法知道节点的个数,因此使用可变长度的链表存储结果
List<Integer> list = new ArrayList<>();
solution(root, list);
if (list == null || list.isEmpty()) {
return null;
}
int[] result = new int[list.size()];
for (int i = 0; i < result.length; i++) {
result[i] = list.get(i);
}
return result;
}
public void solution(TreeNode<Integer> root, List<Integer> list) {
if (root == null) {
return;
}
//递归遍历左子树
solution(root.left, list);
//递归遍历右子树
solution(root.right, list);
list.add(root.data);
}
}