剑指offer-重建二叉树-Java版
重建二叉树
http://www.nowcoder.com/questionTerminal/8a19cbe657394eeaac2f6ea9b0f6fcf6
写在前面
代码说明:代码的下载地址: https://github.com/WuNianLuoMeng/Coding
视频说明:第一次以这样的形式录视频,如果有哪里说的不对,还请各位及时指出,谢谢~
重建二叉树 视频链接
方法一:通过依次遍历前序序列,然后在中序序列中确定当前遍历的前序序列中的数字所在的位置,然后在去划分出当前节点的左右子树,最后在去传入递归程序即可。
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION; class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public class Main4 { private static int index = 0; private static TreeNode solve(int[] pre, int[] tempIn) { int len1 = 0; /// 当前节点的左子树的节点的个数 int len2 = 0; /// 当前节点的右子树的节点的个数 for (int i = 0; i < tempIn.length; i++) { if (pre[index] == tempIn[i]) { break; } len1 ++; /// 左子树节点的个数++ } len2 = tempIn.length - len1 - 1; int index1 = 0; int index2 = 0; int[] temp1 = new int[len1]; /// 当前节点的左子树 int[] temp2 = new int[len2]; /// 当前节点的右子树 boolean flag = false; for (int i = 0; i < tempIn.length; i++) { if (pre[index] == tempIn[i]) { flag = true; } else if (!flag) { temp1[index1++] = tempIn[i]; } else { temp2[index2++] = tempIn[i]; } } TreeNode node = new TreeNode(pre[index]); node.right = null; node.left = null; // System.out.printf("%d左子树:", pre[index]); // for (int i = 0; i < temp1.length; i++) { // System.out.printf("%d ", temp1[i]); // } // System.out.printf(","); // System.out.printf("%d右子树:", pre[index]); // for (int i = 0; i < temp2.length; i++) { // System.out.printf("%d ", temp2[i]); // } // System.out.println(); if (index < pre.length && temp1.length > 0) { index++; /// 遍历前序序列的下标加1 node.left = solve(pre, temp1); /// 创建当前节点的左子树 } if (index < pre.length && temp2.length > 0) { index++; /// 遍历前序序列的下标加1 node.right = solve(pre, temp2); /// 创建当前节点的右子树 } return node; } private static TreeNode reConstructBinaryTree(int[] pre, int[] in) { index = 0; /// 遍历前序序列的下标 return solve(pre, in); } public static void main(String[] args) { int[] pre = {1, 2, 4, 7, 3, 5, 6, 8}; /// 前序遍历 int[] in = {4, 7, 2, 1, 5, 3, 8, 6}; /// 中序遍历 TreeNode root = reConstructBinaryTree(pre, in); dfs1(root); System.out.println(); dfs2(root); System.out.println(); dfs3(root); System.out.println(); } private static void dfs1(TreeNode node) { System.out.printf("%d ", node.val); if (node.left != null) { dfs1(node.left); } if (node.right != null) { dfs1(node.right); } } private static void dfs3(TreeNode node) { if (node.left != null) { dfs3(node.left); } if (node.right != null) { dfs3(node.right); } System.out.printf("%d ", node.val); } private static void dfs2(TreeNode node) { if (node.left != null) { dfs2(node.left); } System.out.printf("%d ", node.val); if (node.right != null) { dfs2(node.right); } } }