简单粗暴
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* }
*/
public class Solution {
private List<TreeNode> preOrder = new ArrayList<>();
private List<TreeNode> inOrder = new ArrayList<>();
private List<TreeNode> postOrder = new ArrayList<>();
/**
*
* @param root TreeNode类 the root of binary tree
* @return int整型二维数组
*/
public int[][] threeOrders (TreeNode root) {
// write code here
pre(root);
in(root);
post(root);
int[][] res = new int[3][preOrder.size()];
for (int i = 0; i < preOrder.size(); i++) {
res[0][i] = preOrder.get(i).val;
}
for (int i = 0; i < inOrder.size(); i++) {
res[1][i] = inOrder.get(i).val;
}
for (int i = 0; i < postOrder.size(); i++) {
res[2][i] = postOrder.get(i).val;
}
return res;
}
public void pre(TreeNode root) {
if (root == null) return;
preOrder.add(root);
pre(root.left);
pre(root.right);
}
public void in(TreeNode root) {
if (root == null) return;
in(root.left);
inOrder.add(root);
in(root.right);
}
public void post(TreeNode root) {
if (root == null) return;
post(root.left);
post(root.right);
postOrder.add(root);
}
}