如题所示
class Solution {
public boolean judgeSymmetric(TreeNode r1, TreeNode r2) {
if(r1 == null && r2 == null) return true;
if(r1 == null && r2 != null) return false;
if(r1 != null && r2 == null) return false;
return r1.val == r2.val && judgeSymmetric(r1.left, r2.right) && judgeSymmetric(r1.right, r2.left);
}
public boolean isSymmetric(TreeNode root) {
return judgeSymmetric(root, root);
}
}
class Solution:
def judge(self, root1, root2):
if not root1 and not root2:
return True
if not root1 or not root2:
return False
return root1.val == root2.val and self.judge(root1.left, root2.right) and self.judge(root1.right, root2.left)
def isSymmetric(self, root: TreeNode) -> bool:
return self.judge(root, root)
原文:https://www.cnblogs.com/MartinLwx/p/15177444.html