其实前序中序后序不过是代码顺序变化而已
import java.util.*; public class Solution { /** * * @param root TreeNode类 the root of binary tree * @return int整型二维数组 */ public int[][] threeOrders (TreeNode root) { ArrayList array = new ArrayList(); per(array,root); mid(array,root); bac(array,root); int[][] a = new int[3][array.size()/3]; // write code here int j = 0; int k = 0; for(int i = 0; i<array.size(); i++){ a[j][k] = (Integer)array.get(i); if(k>=array.size()/3-1){ j++; k = 0; }else{ k++; } } return a ; } public void per(ArrayList arry,TreeNode node){ if(node==null){ return ; } arry.add(node.val); per(arry,node.left); per(arry,node.right); } public void mid(ArrayList arry,TreeNode node){ if(node==null){ return ; } mid(arry,node.left); arry.add(node.val); mid(arry,node.right); } public void bac(ArrayList arry,TreeNode node){ if(node==null){ return ; } bac(arry,node.left); bac(arry,node.right); arry.add(node.val); } }