---
# System prepended metadata

title: 100. Same Tree
tags: [C++, LeetCode-Easy]

---

# 100. Same Tree
Difficulty: Easy

## Solution

```cpp=
/**
*** Author: R-CO
*** E-mail: daniel1820kobe@gmail.com
*** Date: 2020-09-30
**/
#include <cstdlib>
#include <iostream>
using std::cout;
using std::endl;

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
 * right(right) {}
 * };
 */
struct TreeNode {
  int val;
  TreeNode *left;
  TreeNode *right;
  TreeNode() : val(0), left(nullptr), right(nullptr) {}
  TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  TreeNode(int x, TreeNode *left, TreeNode *right)
      : val(x), left(left), right(right) {}
};

struct TestCaseStruct {};

class Solution {
 public:
  bool isSameTree(TreeNode *p, TreeNode *q) {
    if (p == nullptr && q == nullptr) {
      return true;
    } else if ((p == nullptr && q != nullptr) ||
               (p != nullptr && q == nullptr)) {
      return false;
    }

    if (p->val != q->val) {
      return false;
    }

    return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
  }
};

int main(int argc, char *argv[]) { return EXIT_SUCCESS; }

```

## Result

Success
Details 
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Same Tree.
Memory Usage: 10.2 MB, less than 36.30% of C++ online submissions for Same Tree.

###### tags: `LeetCode-Easy` `C++`
