44
55public class Code02_TreeEqual {
66
7- public static class Node {
8- public int value ;
9- public Node left ;
10- public Node right ;
7+ public static class TreeNode {
8+ public int val ;
9+ public TreeNode left ;
10+ public TreeNode right ;
1111
12- public Node (int v ) {
13- value = v ;
12+ public TreeNode (int v ) {
13+ val = v ;
1414}
1515}
1616
17- public static boolean containsTree1 (Node big , Node small ) {
18- if (small == null ) {
19- return true ;
20- }
21- if (big == null ) {
22- return false ;
23- }
24- if (isSameValueStructure (big , small )) {
25- return true ;
26- }
27- return containsTree1 (big .left , small ) || containsTree1 (big .right , small );
28- }
29-
30- public static boolean isSameValueStructure (Node head1 , Node head2 ) {
31- if (head1 == null && head2 != null ) {
32- return false ;
33- }
34- if (head1 != null && head2 == null ) {
35- return false ;
36- }
37- if (head1 == null && head2 == null ) {
38- return true ;
39- }
40- if (head1 .value != head2 .value ) {
41- return false ;
42- }
43- return isSameValueStructure (head1 .left , head2 .left )
44- && isSameValueStructure (head1 .right , head2 .right );
45- }
46-
47- public static boolean containsTree2 (Node big , Node small ) {
17+ // 测试链接 : https://leetcode.cn/problems/subtree-of-another-tree/
18+ // 提交如下代码可以直接通过
19+ public static boolean isSubtree (TreeNode big , TreeNode small ) {
4820if (small == null ) {
4921return true ;
5022}
@@ -65,17 +37,17 @@ public static boolean containsTree2(Node big, Node small) {
6537return getIndexOf (str , match ) != -1 ;
6638}
6739
68- public static ArrayList <String > preSerial (Node head ) {
40+ public static ArrayList <String > preSerial (TreeNode head ) {
6941ArrayList <String > ans = new ArrayList <>();
7042pres (head , ans );
7143return ans ;
7244}
7345
74- public static void pres (Node head , ArrayList <String > ans ) {
46+ public static void pres (TreeNode head , ArrayList <String > ans ) {
7547if (head == null ) {
7648ans .add (null );
7749} else {
78- ans .add (String .valueOf (head .value ));
50+ ans .add (String .valueOf (head .val ));
7951pres (head .left , ans );
8052pres (head .right , ans );
8153}
@@ -134,39 +106,4 @@ public static boolean isEqual(String a, String b) {
134106}
135107}
136108
137- // for test
138- public static Node generateRandomBST (int maxLevel , int maxValue ) {
139- return generate (1 , maxLevel , maxValue );
140- }
141-
142- // for test
143- public static Node generate (int level , int maxLevel , int maxValue ) {
144- if (level > maxLevel || Math .random () < 0.5 ) {
145- return null ;
146- }
147- Node head = new Node ((int ) (Math .random () * maxValue ));
148- head .left = generate (level + 1 , maxLevel , maxValue );
149- head .right = generate (level + 1 , maxLevel , maxValue );
150- return head ;
151- }
152-
153- public static void main (String [] args ) {
154- int bigTreeLevel = 7 ;
155- int smallTreeLevel = 4 ;
156- int nodeMaxValue = 5 ;
157- int testTimes = 100000 ;
158- System .out .println ("test begin" );
159- for (int i = 0 ; i < testTimes ; i ++) {
160- Node big = generateRandomBST (bigTreeLevel , nodeMaxValue );
161- Node small = generateRandomBST (smallTreeLevel , nodeMaxValue );
162- boolean ans1 = containsTree1 (big , small );
163- boolean ans2 = containsTree2 (big , small );
164- if (ans1 != ans2 ) {
165- System .out .println ("Oops!" );
166- }
167- }
168- System .out .println ("test finish!" );
169-
170- }
171-
172109}
0 commit comments