巧妙做法 先得到二叉树的深度 然后将数组填充进去 维护三个类变量即可
实现二叉树先序,中序和后序遍历
http://www.nowcoder.com/questionTerminal/a9fec6c46a684ad5a3abd4e365a9d362
//巧妙做法 先得到二叉树的深度 然后将数组填充进去 维护三个类变量即可
//得到二叉树的 深度 size
public static int getTreeSize(TreeNode node) { if (node == null) { return 0; } return 1 + getTreeSize(node.left) + getTreeSize(node.right); } static int pre = 0, mid = 0, post = 0; public static int[][] threeOrders(TreeNode root) { // write code here int treeSize = getTreeSize(root); int[][] ans = new int[3][treeSize]; threeTree(root, ans); return ans; } public static void threeTree(TreeNode node, int[][] arr) { if (node == null) { return; } arr[0][pre++] = node.val; threeTree(node.left, arr); arr[1][mid++] = node.val; threeTree(node.right, arr); arr[2][post++] = node.val; }