---
tags: leetcode
---
# [101. Symmetric Tree](https://leetcode.com/problems/symmetric-tree/)
---
# My Solution
## The Key Idea for Solving This Coding Question
## C++ Code
```cpp=
/**
* 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) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode *root) {
return isSymmetric(root->left, root->right);
}
private:
bool isSymmetric(TreeNode *t1, TreeNode *t2) {
if (t1 == nullptr && t2 == nullptr) {
return true;
}
if (t1 == nullptr || t2 == nullptr) {
return false;
}
if (t1->val != t2->val) {
return false;
}
return isSymmetric(t1->left, t2->right) && isSymmetric(t1->right, t2->left);
}
};
```
## Time Complexity
$O(n)$
$n$ is the number of nodes in the binary tree referred by `root`.
## Space Complexity
$O(H)$
$H$ is the height of the binary tree referred by `root`.
# Miscellane
<!--
# Test Cases
```
[1,2,2,3,4,4,3]
```
```
[1,2,2,null,3,null,3]
```
```
[1,2,2,2,null,2]
```
```
[1,2,2,3,4,4,6]
```
-->