aboutsummaryrefslogtreecommitdiff
path: root/symmetric_tree.c
blob: f6a1cf3c465001b79ee7afba1273d8e2759b48ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// 101. Symmetric Tree
#include <stdbool.h>
#include <stddef.h>

// given 'root' of binary tree, check whether it is a mirror of itself

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

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

bool isSymmetric(struct TreeNode *root) {
  if (root == NULL)
    return true;
  return traverse(root->left, root->right);
}