public class Solution {/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可** * @param root TreeNode类 * @return int整型一维数组*/public void preorder(List<Integer> list,TreeNode root){if(root == null)return;list.add(root.val);preorder(list,root.left);preorder(list,root.right);}public int[] preorderTraversal (TreeNode root) {// write code hereList<Integer> list = new ArrayList<>();preorder(list,root);int[] res = new int[list.size()];for(int i = 0; i < list.size(); i++){res[i] = list.get(i);//这里用到了自动拆箱}return res;}
}
主要就是preorder方法里的递归