JZ58 对称的二叉树
先序遍历 根左右 同时根右左
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def bfs(self, r1, r2):
if not r1 and not r2:
return True
if not r1 or not r2:
return False
if r1.val == r2.val:
return self.bfs(r1.left, r2.right) and self.bfs(r1.right, r2.left)
return False
def isSymmetrical(self, pRoot):
if not pRoot:
return True
return self.bfs(pRoot.left, pRoot.right)