Java:
class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null) return true;
return isSym(root,root);
}
public boolean isSym( TreeNode root1, TreeNode root2 ){
if (root1 == null || root2 == null) return root1==root2;
if (root1.val != root2.val) return false;
return isSym(root1.left,root2.right) && isSym(root1.right,root2.left);
}
}
Python:
class Solution:
def isSymmetric(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
def isS(root1, root2):
if root1 is None and root2 is None:
return True
if root1 and root2 and root1.val == root2.val:
return isS( root1.left, root2.right ) and isS( root1.right, root2.left )
return False
return isS(root, root)