Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8014ff0

Browse files
refactor 105
1 parent0cc223a commit8014ff0

File tree

2 files changed

+55
-30
lines changed

2 files changed

+55
-30
lines changed

‎src/main/java/com/fishercoder/solutions/_105.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ public static class Solution1 {
1212
* credit: https://discuss.leetcode.com/topic/29838/5ms-java-clean-solution-with-caching use
1313
* HashMap as the cache so that accessing inorder index becomes O(1) time Note: The first
1414
* element of preorder array is the root!
15+
*
16+
* Using a pen and paper to visualize this helps a great deal!
17+
* preorder array has the root at the head, then we could use this number as a pivot in the inorder array: all elements on the left of this pivot should form a left subtree of this pivot
18+
* and anything on the right side of this pivot in the inorder array should form a right subtree of this pivot.
1519
*/
1620
publicTreeNodebuildTree(int[]preorder,int[]inorder) {
1721
Map<Integer,Integer>inorderMap =newHashMap();
@@ -23,14 +27,14 @@ public TreeNode buildTree(int[] preorder, int[] inorder) {
2327
returnbuildTree(preorder,0,preorder.length -1,inorderMap,0,inorder.length -1);
2428
}
2529

26-
privateTreeNodebuildTree(int[]preorder,intpreStart,intpreEnd,
27-
Map<Integer,Integer>inorderMap,intinStart,intinEnd) {
30+
privateTreeNodebuildTree(int[]preorder,intpreStart,intpreEnd,Map<Integer,Integer>inorderMap,intinStart,intinEnd) {
2831
if (preStart >preEnd ||inStart >inEnd) {
2932
returnnull;
3033
}
3134

3235
TreeNoderoot =newTreeNode(preorder[preStart]);
3336
intinRoot =inorderMap.get(preorder[preStart]);
37+
//This line is the key to figure out how many nodes should be on the left subtree
3438
intnumsLeft =inRoot -inStart;
3539

3640
/**It's easy to understand and remember:

‎src/test/java/com/fishercoder/_105Test.java

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,60 @@
33
importcom.fishercoder.common.classes.TreeNode;
44
importcom.fishercoder.common.utils.TreeUtils;
55
importcom.fishercoder.solutions._105;
6+
67
importjava.util.Arrays;
8+
79
importorg.junit.BeforeClass;
810
importorg.junit.Test;
911

1012
importstaticjunit.framework.Assert.assertEquals;
1113

1214
publicclass_105Test {
13-
privatestatic_105.Solution1solution1;
14-
privatestaticTreeNodeexpected;
15-
privatestaticTreeNodeactual;
16-
privatestaticint[]preorder;
17-
privatestaticint[]inorder;
18-
19-
@BeforeClass
20-
publicstaticvoidsetup() {
21-
solution1 =new_105.Solution1();
22-
}
23-
24-
@Test
25-
publicvoidtest1() {
26-
preorder =newint[] {1,2,3};
27-
inorder =newint[] {2,1,3};
28-
actual =solution1.buildTree(preorder,inorder);
29-
expected =TreeUtils.constructBinaryTree(Arrays.asList(1,2,3));
30-
assertEquals(expected,actual);
31-
}
32-
33-
@Test
34-
publicvoidtest2() {
35-
preorder =newint[] {1,2,4,5,3};
36-
inorder =newint[] {4,2,5,1,3};
37-
actual =solution1.buildTree(preorder,inorder);
38-
expected =TreeUtils.constructBinaryTree(Arrays.asList(1,2,3,4,5));
39-
assertEquals(expected,actual);
40-
}
15+
privatestatic_105.Solution1solution1;
16+
privatestaticTreeNodeexpected;
17+
privatestaticTreeNodeactual;
18+
privatestaticint[]preorder;
19+
privatestaticint[]inorder;
20+
21+
@BeforeClass
22+
publicstaticvoidsetup() {
23+
solution1 =new_105.Solution1();
24+
}
25+
26+
@Test
27+
publicvoidtest1() {
28+
preorder =newint[]{1,2,3};
29+
inorder =newint[]{2,1,3};
30+
actual =solution1.buildTree(preorder,inorder);
31+
expected =TreeUtils.constructBinaryTree(Arrays.asList(1,2,3));
32+
assertEquals(expected,actual);
33+
}
34+
35+
@Test
36+
publicvoidtest2() {
37+
preorder =newint[]{1,2,4,5,3};
38+
inorder =newint[]{4,2,5,1,3};
39+
actual =solution1.buildTree(preorder,inorder);
40+
expected =TreeUtils.constructBinaryTree(Arrays.asList(1,2,3,4,5));
41+
assertEquals(expected,actual);
42+
}
43+
44+
@Test
45+
publicvoidtest3() {
46+
preorder =newint[]{3,9,20,15,7};
47+
inorder =newint[]{9,3,15,20,7};
48+
actual =solution1.buildTree(preorder,inorder);
49+
expected =TreeUtils.constructBinaryTree(Arrays.asList(3,9,20,null,null,15,7));
50+
assertEquals(expected,actual);
51+
}
52+
53+
@Test
54+
publicvoidtest4() {
55+
preorder =newint[]{3,1,2,4};
56+
inorder =newint[]{1,2,3,4};
57+
actual =solution1.buildTree(preorder,inorder);
58+
expected =TreeUtils.constructBinaryTree(Arrays.asList(3,1,4,null,2));
59+
TreeUtils.printBinaryTree(expected);
60+
assertEquals(expected,actual);
61+
}
4162
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp