Skip to content

Commit b5a9d78

Browse files
Same tree
Signed-off-by: Leo Ma <begeekmyfriend@gmail.com>
1 parent 09e2050 commit b5a9d78

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

100_same_tree/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test same_tree.c

100_same_tree/same_tree.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}

0 commit comments

Comments
 (0)