JAVA
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整型二维数组
*/
List<Integer> front = new ArrayList<>();
List<Integer> mid = new ArrayList<>();
List<Integer> back = new ArrayList<>();
public int[][] threeOrders (TreeNode root) {
// write code here
frontOrder(root);
midOrder(root);
backOrder(root);
//申明二维数组、
int[][] res = {convertListToArr(front),convertListToArr(mid),convertListToArr(back)};
return res;
}
//先序遍历
public void frontOrder (TreeNode root){
if(root ==null) return;
front.add(root.val);
frontOrder(root.left);
frontOrder(root.right);
}
//中序遍历
public void midOrder (TreeNode root){
if(root ==null) return;
midOrder(root.left);
mid.add(root.val);
midOrder(root.right);
}
//后序遍历
public void backOrder (TreeNode root){
if(root ==null) return;
backOrder(root.left);
backOrder(root.right);
back.add(root.val);
}
//别的转换的接口咱不知道,老老实实写一个list转数组的方法
public int[] convertListToArr (List<Integer> list){
int[] arr = new int[list.size()];
for(int i=0;i<list.size();i++){
arr[i] = list.get(i);
}
return arr;
}
}


京公网安备 11010502036488号