用两个指针去遍历这棵树,(并使用深度优先 中序遍历方法)一个指针从左方向开始遍历,一个指针从右方向开始遍历。比较结构与数据
# 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 isbalance(self, p: Optional[TreeNode], q: Optional[TreeNode]):
if p is None and q is None:
return True
elif p is None or q is None:
return False
elif p.val != q.val:
return False
else:
return self.isbalance(p.left,q.right) and self.isbalance(p.right,q.left)
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
return self.isbalance(root,root)
标签:None,right,return,val,root,self,二叉树,轴对称
From: https://www.cnblogs.com/szj666/p/16796877.html