---
# System prepended metadata

title: 'LeetCode #101 [Symmetric Tree](https://leetcode.com/problems/symmetric-tree/)'
tags: [Recursion, LeetCode, Easy, Tree]

---

###### tags: `LeetCode` `Tree` `Recursion` `Easy`
# LeetCode #101 [Symmetric Tree](https://leetcode.com/problems/symmetric-tree/)
### (Easy)

給定一個二元樹，檢查它是否是鏡像對稱的。


---
使用遞迴法, 每次比對左節點的值是否等於右節點, 以及左節點的左子節點是否等於右節點的右子節點, 以及左節點的右子節點是否等於右節點的左子節點, 如果皆符合, 或是左右節點皆為空節點則回傳true, 其餘則回傳false。


---
```
class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        return isequal(root->right, root->left);
    }
    bool isequal(TreeNode* comp1, TreeNode* comp2){
        if(comp1==nullptr&&comp2==nullptr)return 1;
        else if((comp1&&!comp2)||(!comp1&&comp2))return 0;
        else if(comp1->val==comp2->val&&isequal(comp1->left,comp2->right)&&isequal(comp1->right,comp2->left))return 1;
        return 0;
    }
};
```