101. 对称二叉树
力扣101
给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
1
/ \
2 2
/ \ / \
3 4 4 3
递归
class Solution:
def dfs(self, L: TreeNode, R: TreeNode) -> bool:
if L == None and R == None:
return True
if L == None or R == None:
return False
return (L.val == R.val) and self.dfs(L.left, R.right) and self.dfs(L.right, R.left)
def isSymmetric(self, root: TreeNode) -> bool:
if root == None:
return True
return self.dfs(root.left, root.right)

745

被折叠的 条评论
为什么被折叠?



