-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathSolution.java
65 lines (51 loc) · 1.42 KB
/
Solution.java
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
/**
* @author Oleg Cherednik
* @since 12.12.2018
*/
public class Solution {
public static void main(String... args) {
Node t = new Node(3);
Node root = createTree(t);
System.out.println(isSubtreeEquals(root, t)); // true
}
private static Node createTree(Node t) {
Node four = new Node(4);
four.left = new Node(7);
Node two = new Node(2);
two.right = four;
t.left = new Node(5);
t.right = new Node(6);
Node root = new Node(1);
root.left = two;
root.right = t;
return root;
}
public static boolean isSubtreeEquals(Node s, Node t) {
String strS = serialize(s);
String strT = serialize(t);
return strS.contains(strT);
}
private static String serialize(Node root) {
return dfs(root, new StringBuilder()).toString();
}
private static StringBuilder dfs(Node node, StringBuilder buf) {
if (node != null) {
buf.append(node.data);
buf.append('(');
dfs(node.left, buf);
buf.append(')');
buf.append('(');
dfs(node.right, buf);
buf.append(')');
}
return buf;
}
private static final class Node {
private final int data;
private Node left;
private Node right;
public Node(int data) {
this.data = data;
}
}
}