Skip to content

Commit a72850b

Browse files
committed
add 重构二叉树
1 parent ba23f47 commit a72850b

19 files changed

Lines changed: 112 additions & 11 deletions

Algorithm/剑指offer/coding-interviews/.idea/workspace.xml

Lines changed: 16 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Algorithm/剑指offer/coding-interviews/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@
77
<groupId>com.leosanqing</groupId>
88
<artifactId>coding-interviews</artifactId>
99
<version>1.0-SNAPSHOT</version>
10+
<build>
11+
<plugins>
12+
<plugin>
13+
<groupId>org.apache.maven.plugins</groupId>
14+
<artifactId>maven-compiler-plugin</artifactId>
15+
<configuration>
16+
<source>6</source>
17+
<target>6</target>
18+
</configuration>
19+
</plugin>
20+
</plugins>
21+
</build>
1022

1123

1224
</project>

Algorithm/剑指offer/coding-interviews/src/main/java/com/leosanqing/algorithm/_6_PrintListFromTailToHead.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static void main(String[] args) {
2929
* 使用递归来打印
3030
*
3131
* 如果像书上使用其他辅助容器,那基本所有容器都可以。
32-
* @param head
32+
* @param head 头结点
3333
*/
3434
private static void method(Node head) {
3535
if (head != null) {
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.leosanqing.algorithm;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* @Author: leosanqing
7+
* @Date: 2019-11-09 11:44
8+
*
9+
*
10+
* 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。
11+
*
12+
* 假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
13+
* 例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},
14+
* 则重建二叉树并返回头结点。
15+
*
16+
*/
17+
public class _7_ReConstructBinaryTree {
18+
public static void main(String[] args) {
19+
20+
int[] pre={1,2,4,7,3,5,6,8};
21+
int[] in ={4,7,2,1,5,3,8,6};
22+
23+
System.out.println(method(pre,in).value);
24+
}
25+
26+
/**
27+
* !!!!!注意:题目中说,数组中的数字是不会重复的。不然用不了下面的方法
28+
*
29+
* 这道题主要运用 二叉树先序遍历和中序遍历的特点
30+
*
31+
* 1.先序遍历中,第一个就是根节点。按照上面的例子,也就是 1
32+
* 2.然后我们再去中序遍历的 数组中找到这个 1 。
33+
* 在1前面的那部分就是 这个根节点的左子树{4,7,2}。右边的为右子树{5,3,8,6}
34+
* 3.然后我们再在左子树中找到第一个数 4,再重复上述步骤
35+
*
36+
* 这样我们就能找到树的结构,然后一直递归。再在这个过程中添加上左右子节点。就能完成重构了
37+
*
38+
* @param pre 左子树
39+
* @param in 右子树
40+
* @return 根节点
41+
*/
42+
private static TreeNode method(int[] pre,int[] in){
43+
if(null == pre||null == in){
44+
return null;
45+
}
46+
if(pre.length <=0||in.length<=0) {
47+
return null;
48+
}
49+
if(pre.length!=in.length){
50+
return null;
51+
}
52+
53+
TreeNode root = new TreeNode(pre[0]);
54+
for (int i = 0; i < pre.length; i++) {
55+
// 找到中序遍历的根节点
56+
if(pre[0] == in[i]){
57+
58+
root.left = method(Arrays.copyOfRange(pre,i,i+1),Arrays.copyOfRange(in,0,i));
59+
root.right = method(Arrays.copyOfRange(pre,i+1,pre.length),Arrays.copyOfRange(in,i+1,in.length));
60+
61+
// root.left = reConstructBinaryTree(
62+
// Arrays.copyOfRange(pre,1,i+1),Arrays.copyOfRange(in,0,i));
63+
// root.right = reConstructBinaryTree(
64+
// Arrays.copyOfRange(pre,i+1,pre.length),Arrays.copyOfRange(in,i+1,in.length));
65+
}
66+
}
67+
return root;
68+
}
69+
70+
71+
/**
72+
* 树结构
73+
*/
74+
static class TreeNode{
75+
int value;
76+
TreeNode left;
77+
TreeNode right;
78+
79+
public TreeNode(int value) {
80+
this.value = value;
81+
}
82+
}
83+
}
Binary file not shown.

0 commit comments

Comments
 (0)