aboutsummaryrefslogtreecommitdiff
path: root/same_tree.c
blob: 98320c58c4359d47ee1969b7aea62e6d17c10a2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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;
}