aboutsummaryrefslogtreecommitdiff
path: root/same_tree.c
diff options
context:
space:
mode:
Diffstat (limited to 'same_tree.c')
-rw-r--r--same_tree.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/same_tree.c b/same_tree.c
new file mode 100644
index 0000000..98320c5
--- /dev/null
+++ b/same_tree.c
@@ -0,0 +1,23 @@
+// 100. Same Tree
+#include "leetcode.h"
+
+/*
+ * given the roots of two binary trees 'p' and 'q', write a function to check if
+ * they are the same or not. two binary trees are considered the same if they
+ * are structurally identical, and the nodes have the same value.
+ */
+
+struct TreeNode {
+ int val;
+ struct TreeNode *left, *right;
+};
+
+bool isSameTree(struct TreeNode *p, struct TreeNode *q) {
+ if (!p && !q)
+ return 1;
+ else if (!p || !q)
+ return 0;
+ return p->val == q->val
+ ? isSameTree(p->left, q->left) && isSameTree(p->right, q->right)
+ : 0;
+}