import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * public TreeNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型一维数组 */ private static ArrayList<Integer> arrayList = new ArrayList<>(); public int[] inorderTraversal(TreeNode root) { search(root); int [] result = new int[arrayList.size()]; for (int i = 0; i < arrayList.size(); i++) { result[i] = arrayList.get(i); } return result; } private void search(TreeNode root) { if (root == null) { return; } search(root.left); arrayList.add(root.val); search(root.right); } }
本题知识点分析:
1.二叉树遍历
2.DFS深度优先搜索
3.集合存取
4.集合转数组
本题解题思路分析:
1.创建集合存取中序遍历结果
2.集合转数组进行返回