# LC 101. Symmetric Tree ### [Problem link](https://leetcode.com/problems/symmetric-tree/) ###### tags: `leedcode` `python` `easy` `Binary Tree` Given the <code>root</code> of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center). **Example 1:** <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg" style="width: 354px; height: 291px;" /> ``` Input: root = [1,2,2,3,4,4,3] Output: true ``` **Example 2:** <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg" style="width: 308px; height: 258px;" /> ``` Input: root = [1,2,2,null,3,null,3] Output: false ``` **Constraints:** - The number of nodes in the tree is in the range <code>[1, 1000]</code>. - <code>-100 <= Node.val <= 100</code> **Follow up:** Could you solve it both recursively and iteratively? ## Solution 1 - DFS (recursively) #### Python ```python= # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def isSymmetric(self, root: Optional[TreeNode]) -> bool: def dfs(left, right): if left and right and left.val == right.val: return dfs(left.left, right.right) and dfs(left.right, right.left) return left == right return dfs(root.left, root.right) ``` #### C++ ```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 dfs(TreeNode *left, TreeNode *right) { if (left == nullptr || right == nullptr) { return left == right; } return left->val == right->val && dfs(left->left, right->right) && dfs(left->right, right->left); } bool isSymmetric(TreeNode* root) { return dfs(root->left, root->right); } }; ``` ## Solution 2 - DFS (iteratively) #### Python ```python= # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def isSymmetric(self, root: Optional[TreeNode]) -> bool: q = deque([root.left, root.right]) while q: left = q.pop() right = q.pop() if not left and not right: continue if not left or not right or left.val != right.val: return False q.append(left.left) q.append(right.right) q.append(left.right) q.append(right.left) return True ``` #### C++ ```cpp= class Solution { public: bool isSymmetric(TreeNode* root) { queue<TreeNode *> q; q.push(root->left); q.push(root->right); while (!q.empty()) { TreeNode *left = q.front(); q.pop(); TreeNode *right = q.front(); q.pop(); if (left == nullptr && right == nullptr) { continue; } if (left == nullptr || right == nullptr || (left->val != right->val)) { return false; } q.push(left->left); q.push(right->right); q.push(left->right); q.push(right->left); } return true; } }; ``` >### Complexity >| | Time Complexity | Space Complexity | >| ----------- | --------------- | ---------------- | >| Solution 1 | O(n) | O(n) | >| Solution 2 | O(n) | O(n) | ## Note [類似LC 100. Same Tree](https://hackmd.io/@Alone0506/BkJywty3h)