import java.util.*; import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * } */ public class Solution { /** * * @param root TreeNode类 the root of binary tree * @return int整型二维数组 */ public int[][] threeOrders (TreeNode root) { // write code here if(root == null){ return new int[3][0]; } ArrayList<Integer> list1 = preOrder(root); ArrayList<Integer> list2 = inOrder(root); ArrayList<Integer> list3 = postOrder(root); int[][] res = new int[3][list1.size()]; for(int i = 0; i < list1.size(); ++i){ res[0][i] = list1.get(i); } for(int i = 0; i < list2.size(); ++i){ res[1][i] = list2.get(i); } for(int i = 0; i < list3.size(); ++i){ res[2][i] = list3.get(i); } return res; } public ArrayList<Integer> preOrder(TreeNode root){ ArrayList<Integer> res = new ArrayList<>(); res.add(root.val); if(root.left != null){ res.addAll(preOrder(root.left)); } if(root.right != null){ res.addAll(preOrder(root.right)); } return res; } public ArrayList<Integer> inOrder(TreeNode root){ ArrayList<Integer> res = new ArrayList<>(); if(root.left != null){ res.addAll(inOrder(root.left)); } res.add(root.val); if(root.right != null){ res.addAll(inOrder(root.right)); } return res; } public ArrayList<Integer> postOrder(TreeNode root){ ArrayList<Integer> res = new ArrayList<>(); if(root.left != null){ res.addAll(postOrder(root.left)); } if(root.right != null){ res.addAll(postOrder(root.right)); } res.add(root.val); return res; } }