Description:
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

    1
/ \
2 2
/ \ / \
3 4 4 3

But the following [1,2,2,null,3,null,3] is not:

   1
/ \
2 2
\ \
3 3

题意:判断一颗二叉树是否为一颗镜像二叉树

解法:镜像二叉树如图所示,其左子树的所有节点翻转后与右子树对应节点相同;因此,我们只需要递归的计算二叉树的根节点与其左右两个子树的节点,相对应翻转后是否相同;

LeetCode-Symmetric Tree_递归

Java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
return isMirror(root, root);
}
private boolean isMirror(TreeNode root1, TreeNode root2) {
if (root1 == null && root2 == null) return true;
if (root1 == null || root2 == null) return false;
if (root1.val != root2.val) return false;
return isMirror(root1.left, root2.right) && isMirror(root1.right, root2.left);
}

}