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

Commite9a3f0f

Browse files
refactor 652
1 parent50f9186 commite9a3f0f

File tree

2 files changed

+21
-117
lines changed

2 files changed

+21
-117
lines changed

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

Lines changed: 16 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
importjava.util.Queue;
1212
importjava.util.Set;
1313

14-
1514
/**
1615
* 652. Find Duplicate Subtrees
1716
*
@@ -37,126 +36,31 @@
3736
Therefore, you need to return above trees' root in the form of a list.
3837
*/
3938
publicclass_652 {
39+
publicstaticclassSolution1 {
4040

41-
/**credit: https://discuss.leetcode.com/topic/97584/java-concise-postorder-traversal-solution*/
42-
43-
/**You don't actually need to check if every other tree is a duplicate of current node,
44-
* just when you go through each node, you'll see whether there's already one in the map,
45-
* since map.containsKey() checks this TreeNode.*/
46-
publicList<TreeNode>findDuplicateSubtrees(TreeNoderoot) {
47-
List<TreeNode>res =newLinkedList<>();
48-
postorder(root,newHashMap<>(),res);
49-
returnres;
50-
}
51-
52-
privateStringpostorder(TreeNodecurr,HashMap<String,Integer>map,List<TreeNode>res) {
53-
if (curr ==null) {
54-
return"#";
55-
}
56-
Stringserial =curr.val +"," +postorder(curr.left,map,res) +"," +postorder(curr.right,map,res);
57-
if (map.getOrDefault(serial,0) ==1) {
58-
res.add(curr);
59-
}
60-
map.put(serial,map.getOrDefault(serial,0) +1);
61-
returnserial;
62-
}
63-
64-
65-
publicclassMyOriginalSolution {
66-
/**This solution is blocked at [2,1,1] test case and I've asked a question here:
67-
* https://discuss.leetcode.com/topic/97746/oj-bug-for-test-case-2-1-1-or-somewhere-my-code-is-wrong*/
41+
/**credit: https://discuss.leetcode.com/topic/97584/java-concise-postorder-traversal-solution*/
6842

6943
/**
70-
*Use BFS to traverse each node, at this time, put each node into Map as key (except rootnode since root won't have duplicates),
71-
*then use DFS to visit all of its siblings to find possible duplite subtrees,
72-
*because duplicate could only possibly be found in siblings or sibling's children.
44+
*You don't actually need to check if every other tree is a duplicate of currentnode,
45+
*just when you go through each node, you'll see whether there's already one in the map,
46+
*since map.containsKey() checks this TreeNode.
7347
*/
7448
publicList<TreeNode>findDuplicateSubtrees(TreeNoderoot) {
75-
List<TreeNode>result =newArrayList<>();
76-
if (root ==null) {
77-
returnresult;
78-
}
79-
Map<TreeNode,List<TreeNode>>map =newHashMap<>();
80-
Queue<TreeNode>oldQueue =newLinkedList<>();
81-
Queue<TreeNode>newQueue =newLinkedList<>();
82-
oldQueue.offer(root);
83-
while (!oldQueue.isEmpty()) {
84-
intsize =oldQueue.size();
85-
for (inti =0;i <size;i++) {
86-
TreeNodecurr =oldQueue.poll();
87-
if (curr.left !=null) {
88-
newQueue.offer(curr.left);
89-
}
90-
if (curr.right !=null) {
91-
newQueue.offer(curr.right);
92-
}
93-
if (curr !=root) {
94-
if (!map.containsKey(curr)) {
95-
map.put(curr,newArrayList<>());
96-
}
97-
}
98-
}
99-
for (TreeNodetreeNode :newQueue) {
100-
findDuplicateSubtrees(treeNode,newQueue,map);
101-
}
102-
oldQueue =newQueue;
103-
}
104-
Set<TreeNode>set =newHashSet<>();
105-
for (Map.Entry<TreeNode,List<TreeNode>>entry :map.entrySet()) {
106-
if (entry.getValue().size() >0) {
107-
set.add(entry.getKey());
108-
}
109-
}
110-
result.addAll(set);
111-
returnresult;
49+
List<TreeNode>res =newLinkedList<>();
50+
postorder(root,newHashMap<>(),res);
51+
returnres;
11252
}
11353

114-
privatevoidfindDuplicateSubtrees(TreeNodetreeNode,Queue<TreeNode>newQueue,Map<TreeNode,List<TreeNode>>map) {
115-
for (TreeNodetn :newQueue) {
116-
if (treeNode !=tn) {
117-
if (isSubtree(tn,treeNode)) {
118-
List<TreeNode>list =map.getOrDefault(treeNode,newArrayList<>());
119-
list.add(tn);
120-
map.put(treeNode,list);
121-
return;
122-
}
123-
}
54+
privateStringpostorder(TreeNodecurr,HashMap<String,Integer>map,List<TreeNode>res) {
55+
if (curr ==null) {
56+
return"#";
12457
}
125-
}
126-
127-
privatebooleanisSubtree(TreeNodes,TreeNodet) {
128-
if (s ==null &&t ==null) {
129-
returntrue;
130-
}
131-
booleanisSubTree =false;
132-
if (s !=null &&t !=null &&s.val ==t.val) {
133-
isSubTree =isSameTree(s,t);
134-
}
135-
if (isSubTree) {
136-
returntrue;
137-
}
138-
booleanisSubTreeLeft =false;
139-
if (s.left !=null) {
140-
isSubTreeLeft =isSubtree(s.left,t);
141-
}
142-
if (isSubTreeLeft) {
143-
returntrue;
144-
}
145-
booleanisSubTreeRight =false;
146-
if (s.right !=null) {
147-
isSubTreeRight =isSubtree(s.right,t);
148-
}
149-
if (isSubTreeRight) {
150-
returntrue;
151-
}
152-
returnfalse;
153-
}
154-
155-
privatebooleanisSameTree(TreeNodep,TreeNodeq) {
156-
if (p ==null ||q ==null) {
157-
returnp ==q;
58+
Stringserial =curr.val +"," +postorder(curr.left,map,res) +"," +postorder(curr.right,map,res);
59+
if (map.getOrDefault(serial,0) ==1) {
60+
res.add(curr);
15861
}
159-
returnp.val ==q.val &&isSameTree(p.left,q.left) &&isSameTree(p.right,q.right);
62+
map.put(serial,map.getOrDefault(serial,0) +1);
63+
returnserial;
16064
}
16165
}
16266
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
importstaticorg.junit.Assert.assertEquals;
1414

1515
publicclass_652Test {
16-
privatestatic_652test;
16+
privatestatic_652.Solution1solution1;
1717
privatestaticList<TreeNode>expected;
1818
privatestaticTreeNoderoot;
1919

2020
@BeforeClass
2121
publicstaticvoidsetup() {
22-
test =new_652();
22+
solution1 =new_652.Solution1();
2323
}
2424

2525
@Before
@@ -41,13 +41,13 @@ public void test1() {
4141
tree1.left =newTreeNode(4);
4242
TreeNodetree2 =newTreeNode(4);
4343
expected =newArrayList<>(Arrays.asList(tree2,tree1));
44-
assertEquals(expected,test.findDuplicateSubtrees(root));
44+
assertEquals(expected,solution1.findDuplicateSubtrees(root));
4545
}
4646

4747
@Test
4848
publicvoidtest2() {
4949
expected =newArrayList<>();
50-
assertEquals(expected,test.findDuplicateSubtrees(root));
50+
assertEquals(expected,solution1.findDuplicateSubtrees(root));
5151
}
5252

5353
@Test
@@ -58,6 +58,6 @@ public void test3() {
5858

5959
TreeNodetree1 =newTreeNode(1);
6060
expected =newArrayList<>(Arrays.asList(tree1));
61-
assertEquals(expected,test.findDuplicateSubtrees(root));
61+
assertEquals(expected,solution1.findDuplicateSubtrees(root));
6262
}
6363
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp