先序:根左右
中序:左根右
后序:左右根
按照这个顺序进行递归遍历
最后进行整合返回成数组,这里参看我的另一个题解
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整型二维数组 */ ArrayList<Integer> One = new ArrayList<Integer>(); ArrayList<Integer> Two = new ArrayList<Integer>(); ArrayList<Integer> Three = new ArrayList<Integer>(); public int[][] threeOrders(TreeNode root) { // write code here first(root); Second(root); Third(root); int n=One.size(); int[][] count=new int[3][n]; Integer[] one=One.toArray(new Integer[n]); Integer[] two=Two.toArray(new Integer[n]); Integer[] three=Three.toArray(new Integer[n]); for (int i = 0; i < n; i++) { count[0][i]=one[i]; count[1][i]=two[i]; count[2][i]=three[i]; } return count; } void first(TreeNode root) { if (root==null) return; One.add(root.val); first(root.left); first(root.right); } void Second(TreeNode root) { if (root==null) return; Second(root.left); Two.add(root.val); Second(root.right); } void Third(TreeNode root) { if (root==null) return; Third(root.left); Third(root.right); Three.add(root.val); } }