对称二叉树
程序员文章站
2022-06-17 19:49:36
...
Python
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def isSymmetric(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if not root:
return True
def dfs(left,right):
if not (left or right):
return True
if not (left and right):
return False
if left.val!=right.val:
return False
return dfs(left.left,right.right) and dfs(left.right,right.left)
return dfs(root.left,root.right)
上一篇: LeetCode 4.寻找两个正序数组的中位数 (二分查找)
下一篇: 有效的括号