File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change
1
+ all :
2
+ gcc -O2 -o test same_tree.c
Original file line number Diff line number Diff line change
1
+ #include <stdio.h>
2
+ #include <stdlib.h>
3
+ #include <stdbool.h>
4
+
5
+ struct TreeNode {
6
+ int val ;
7
+ struct TreeNode * left ;
8
+ struct TreeNode * right ;
9
+ };
10
+
11
+ bool isSameTree (struct TreeNode * p , struct TreeNode * q ) {
12
+ if ((p == NULL && q != NULL ) || (p != NULL && q == NULL )) {
13
+ return false;
14
+ }
15
+ if (p != NULL && q != NULL ) {
16
+ if (p -> val != q -> val ) {
17
+ return false;
18
+ }
19
+ if (!isSameTree (p -> left , q -> left )) {
20
+ return false;
21
+ }
22
+ if (!isSameTree (p -> right , q -> right )) {
23
+ return false;
24
+ }
25
+ }
26
+
27
+ return true;
28
+ }
29
+
30
+ int main (void )
31
+ {
32
+ printf ("%s\n" , isSameTree (NULL , NULL ) ? "true" : "false" );
33
+ return 0 ;
34
+ }
You can’t perform that action at this time.
0 commit comments