-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathBT_Problem_24.java
More file actions
66 lines (54 loc) · 1.84 KB
/
Copy pathBT_Problem_24.java
File metadata and controls
66 lines (54 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package trees.binaryTree;
import java.util.*;
// Problem Title => Check if a Binary Tree contains duplicate subtrees of size 2 or more [ IMP ]
public class BT_Problem_24 {
static char MARKER = '$';
static class Node{
int data;
Node left,right;
Node(int item){
this.data = item;
}
}
public static String dupSubUtil(Node root, HashSet<String> subtrees){
String s = "";
// If current node is NULL, return marker
if (root == null)
return s + MARKER;
// If left subtree has a duplicate subtree.
String lStr = dupSubUtil(root.left,subtrees);
if (lStr.equals(s))
return s;
// Do same for right subtree
String rStr = dupSubUtil(root.right,subtrees);
if (rStr.equals(s))
return s;
// Serialize current subtree
s = s + root.data + lStr + rStr;
// If current subtree already exists in hash table.
// [Note that size of a serialized tree with single node is 3 as it has two marker nodes].
if (s.length() > 3 && subtrees.contains(s))
return "";
subtrees.add(s);
return s;
}
public static String dupSub(Node root){
HashSet<String> subtrees=new HashSet<>();
return dupSubUtil(root,subtrees);
}
public static void main(String[] args) {
Node root = new Node('A');
root.left = new Node('B');
root.right = new Node('C');
root.left.left = new Node('D');
root.left.right = new Node('E');
root.right.right = new Node('B');
root.right.right.right = new Node('E');
root.right.right.left= new Node('D');
String str = dupSub(root);
if(str.equals(""))
System.out.print(" Yes ");
else
System.out.print(" No ");
}
}