# Leetcode 101. Symmetric Tree (C語言)
- 題目
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
- 範例
```
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following [1,2,2,null,3,null,3] is not:
1
/ \
2 2
\ \
3 3
```
```c
bool isMirror(struct TreeNode *t1,struct TreeNode *t2){
if(!t1&&!t2)return true;
if(!t1||!t2)return false;
return ((t1->val==t2->val)&&isMirror(t1->left,t2->right)&&isMirror(t1->right,t2->left));
}
bool isSymmetric(struct TreeNode* root){
if(!root)return true;
return isMirror(root->left,root->right);
}
```
思路:
對稱包含幾點
1. root值相同
2. 左圖之左子樹(藍)=右圖之右子樹(藍)
3. 左圖之右子樹(黃)=右圖之左子樹(黃)
所以root先判斷是否為空,空樹必對稱回傳true,否則將左右子樹丟去檢查。
檢查函式(isMirror)邏輯如下
1. t1與t2為空,則對稱return true。
2. t1與t2只有一者為空,則不對稱return false。
3. t1與t2皆不為空,進入判斷
3.1 root值是否相同?(判斷root值)
3.2 t1左與t2右是否相同?(判斷藍色塊)
3.3 t1右與t2左是否相同?(判斷黃色塊)