对称二叉树的本质首先搞清楚,分三种情况
1.在第二层:pRoot.left = pRoot.right
2.在第三层开始,root1.left == root2.right, root1.right == root2.left
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isSymmetrical(self, pRoot):
# write code here
def dfs(root1, root2):
if not root1 and not root2:
return True
if not root1 or not root2:
return False
if root1.val == root2.val and dfs(root1.left, root2.right) and dfs(root1.right, root2.left):
return True
else:
return False
if not pRoot: return True
return dfs(pRoot.left, pRoot.right)