import java.util.*;
public class Main {
/**
//该段代码仅用于调试,提交时请注释该段代码
class TreeNode<T> {
public T data;
public TreeNode<T> left;
public TreeNode<T> right;
}
*/
public int[] solution(TreeNode<Integer> root) {
if (root == null) return new int[0];
List<Integer> res = new LinkedList<>();
LinkedList<TreeNode<Integer>> list = new LinkedList<>();
TreeNode<Integer> curt = root;
while (curt != null || !list.isEmpty()) {
while(curt != null) {
list.addLast(curt);
curt = curt.left;
}
curt = list.pollLast();
res.add(curt.data);
curt = curt.right;
}
int[] ret = new int[res.size()];
for (int i = 0; i < ret.length; i ++ ) {
ret[i] = res.get(i);
}
return ret;
}
}