# Leetcode --- Symmetric Tree (unedited) ## 101. Symmetric Tree ### Description: Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center). :::info ::: :::warning :::  ### 解法條列 1. 暴力解 ### 解法細節 :::success level search(BFS) queue ::: ### Python Solution 解 ```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: queue = [root] temp_queue = [] level = [] while(queue): node = queue.pop() if(not node): level.append(None) if(not queue): if(not (level == level[::-1])): return False queue, temp_queue = temp_queue, queue level.clear() continue level.append(node.val) temp_queue.insert(0,node.left) temp_queue.insert(0,node.right) if(not queue): if(not (level == level[::-1])): return False queue, temp_queue = temp_queue, queue level.clear() if(not (level == level[::-1])): return False return True ``` --- ```python=3 ``` --- ```python=8 ``` --- ```python=9 ``` --- ```python=15 ``` --- ###### tags: `leetcode` `tree` `medium` `queue` `BFS`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up