1
- @ @ -1 ,1260 +0 ,0 @ @
2
1
package Algorithms .tree ;
3
2
4
3
import java .util .ArrayList ;
5
4
import java .util .Iterator ;
6
5
import java .util .LinkedList ;
6
+ import java .util .List ;
7
7
import java .util .Queue ;
8
8
import java .util .Stack ;
9
9
32
32
* LAC 求解最小公共祖先, 使用list来存储path.
33
33
* LCABstRec 递归求解BST树.
34
34
* LCARec 递归算法 .
35
- *
36
35
* 12. 求二叉树中节点的最大距离:getMaxDistanceRec
36
+ *
37
37
* 13. 由前序遍历序列和中序遍历序列重建二叉树:rebuildBinaryTreeRec
38
38
* 14. 判断二叉树是不是完全二叉树:isCompleteBinaryTree, isCompleteBinaryTreeRec
39
39
* 15. 找出二叉树中最长连续子串(即全部往左的连续节点,或是全部往右的连续节点)findLongest
@@ -98,6 +98,12 @@ public static void main(String[] args) {
98
98
99
99
TreeNode t7 =new TreeNode (0 );
100
100
101
+ TreeNode t8 =new TreeNode (0 );
102
+ TreeNode t9 =new TreeNode (0 );
103
+ TreeNode t10 =new TreeNode (0 );
104
+ TreeNode t11 =new TreeNode (0 );
105
+
106
+
101
107
t1 .left =t2 ;
102
108
t1 .right =t3 ;
103
109
t2 .left =t4 ;
@@ -106,6 +112,12 @@ public static void main(String[] args) {
106
112
107
113
t4 .left =t7 ;
108
114
115
+ // test distance
116
+ t5 .right =t8 ;
117
+ t8 .right =t9 ;
118
+ t9 .right =t10 ;
119
+ t10 .right =t11 ;
120
+
109
121
/*
110
122
10
111
123
/ \
@@ -115,18 +127,20 @@ public static void main(String[] args) {
115
127
/
116
128
0
117
129
*/
118
- System .out .println (LCABstRec (t1 ,t2 ,t4 ).val );
119
- System .out .println (LCABstRec (t1 ,t2 ,t6 ).val );
120
- System .out .println (LCABstRec (t1 ,t4 ,t6 ).val );
121
- System .out .println (LCABstRec (t1 ,t4 ,t7 ).val );
122
- System .out .println (LCABstRec (t1 ,t3 ,t6 ).val );
123
-
124
- System .out .println (LCA (t1 ,t2 ,t4 ).val );
125
- System .out .println (LCA (t1 ,t2 ,t6 ).val );
126
- System .out .println (LCA (t1 ,t4 ,t6 ).val );
127
- System .out .println (LCA (t1 ,t4 ,t7 ).val );
128
- System .out .println (LCA (t1 ,t3 ,t6 ).val );
129
- System .out .println (LCA (t1 ,t6 ,t6 ).val );
130
+ // System.out.println(LCABstRec(t1, t2, t4).val);
131
+ // System.out.println(LCABstRec(t1, t2, t6).val);
132
+ // System.out.println(LCABstRec(t1, t4, t6).val);
133
+ // System.out.println(LCABstRec(t1, t4, t7).val);
134
+ // System.out.println(LCABstRec(t1, t3, t6).val);
135
+ //
136
+ // System.out.println(LCA(t1, t2, t4).val);
137
+ // System.out.println(LCA(t1, t2, t6).val);
138
+ // System.out.println(LCA(t1, t4, t6).val);
139
+ // System.out.println(LCA(t1, t4, t7).val);
140
+ // System.out.println(LCA(t1, t3, t6).val);
141
+ // System.out.println(LCA(t1, t6, t6).val);
142
+
143
+ System .out .println (getMaxDistanceRec (t1 ));
130
144
131
145
//System.out.println(isSame(r1, t1));
132
146
@@ -1185,6 +1199,90 @@ public static boolean LCAPath(TreeNode root, TreeNode node, ArrayList<TreeNode>
1185
1199
// found
1186
1200
return true ;
1187
1201
}
1202
+
1203
+ /*
1204
+ * * 12. 求二叉树中节点的最大距离:getMaxDistanceRec
1205
+ *
1206
+ * 首先我们来定义这个距离:
1207
+ * 距离定义为:两个节点间边的数目.
1208
+ * 如:
1209
+ * 1
1210
+ * / \
1211
+ * 2 3
1212
+ * \
1213
+ * 4
1214
+ * 这里最大距离定义为2,4的距离,为3.
1215
+ * 求二叉树中节点的最大距离 即二叉树中相距最远的两个节点之间的距离。 (distance / diameter)
1216
+ * 递归解法:
1217
+ * 返回值设计:
1218
+ * 返回1. 深度, 2. 当前树的最长距离
1219
+ * (1) 计算左子树的深度,右子树深度,左子树独立的链条长度,右子树独立的链条长度
1220
+ * (2) 最大长度为三者之最:
1221
+ * a. 通过根节点的链,为左右深度+2
1222
+ * b. 左子树独立链
1223
+ * c. 右子树独立链。
1224
+ *
1225
+ * (3)递归初始条件:
1226
+ * 当root == null, depth = -1.maxDistance = -1;
1227
+ *
1228
+ */
1229
+ public static int getMaxDistanceRec (TreeNode root ) {
1230
+ return getMaxDistanceRecHelp (root ).maxDistance ;
1231
+ }
1232
+
1233
+ public static Result getMaxDistanceRecHelp (TreeNode root ) {
1234
+ Result ret =new Result (-1 , -1 );
1235
+
1236
+ if (root ==null ) {
1237
+ return ret ;
1238
+ }
1239
+
1240
+ Result left =getMaxDistanceRecHelp (root .left );
1241
+ Result right =getMaxDistanceRecHelp (root .right );
1242
+
1243
+ // 深度应加1, the depth from the subtree to the root.
1244
+ ret .depth =Math .max (left .depth ,right .depth ) +1 ;
1245
+
1246
+ // 左子树,右子树与根的距离都要加1,所以通过根节点的路径为两边深度+2
1247
+ int crossLen =left .depth +right .depth +2 ;
1248
+
1249
+ // 求出cross根的路径,及左右子树的独立路径,这三者路径的最大值。
1250
+ ret .maxDistance =Math .max (left .maxDistance ,right .maxDistance );
1251
+ ret .maxDistance =Math .max (ret .maxDistance ,crossLen );
1252
+
1253
+ return ret ;
1254
+ }
1255
+
1256
+
1257
+ private static class Result {
1258
+ int depth ;
1259
+ int maxDistance ;
1260
+ public Result (int depth ,int maxDistance ) {
1261
+ this .depth =depth ;
1262
+ this .maxDistance =maxDistance ;
1263
+ }
1264
+ }
1265
+
1266
+ /*
1267
+ * 13. 由前序遍历序列和中序遍历序列重建二叉树:rebuildBinaryTreeRec
1268
+ * */
1269
+ public static TreeNode rebuildBinaryTreeRec (List <Integer >preOrder ,List <Integer >inOrder ) {
1270
+ if (preOrder ==null ||inOrder ==null ) {
1271
+ return null ;
1272
+ }
1273
+
1274
+ // If the traversal is empty, just return a NULL.
1275
+ if (preOrder .size () ==0 ||inOrder .size () ==0 ) {
1276
+ return null ;
1277
+ }
1278
+
1279
+ // we can get the root from the preOrder. Because the first one is the
1280
+ // root.
1281
+ TreeNode root =preOrder .get (0 );
1282
+
1283
+ return null ;
1284
+
1285
+ }
1188
1286
1189
1287
/*
1190
1288
* 15. findLongest