Skip to content

Commit

Permalink
Same tree
Browse files Browse the repository at this point in the history
Signed-off-by: Leo Ma <[email protected]>
  • Loading branch information
begeekmyfriend committed Oct 7, 2017
1 parent 09e2050 commit b5a9d78
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 100_same_tree/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test same_tree.c
34 changes: 34 additions & 0 deletions 100_same_tree/same_tree.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};

bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
if ((p == NULL && q != NULL) || (p != NULL && q == NULL)) {
return false;
}
if (p != NULL && q != NULL) {
if (p->val != q->val) {
return false;
}
if (!isSameTree(p->left, q->left)) {
return false;
}
if (!isSameTree(p->right, q->right)) {
return false;
}
}

return true;
}

int main(void)
{
printf("%s\n", isSameTree(NULL, NULL) ? "true" : "false");
return 0;
}

0 comments on commit b5a9d78

Please sign in to comment.