给你一个二叉树的根节点
root
, 检查它是否轴对称。示例 1:
输入:root = [1,2,2,3,4,4,3] 输出:true示例 2:
输入:root = [1,2,2,null,3,null,3] 输出:false提示:
- 树中节点数目在范围
[1, 1000]
内-100 <= Node.val <= 100
- 全部代码
/**
* @author happy
* 对称二叉树
* 给你一个二叉树的根节点 root , 检查它是否轴对称。
*
*/
public class Num101_isSymmetricTree {
public boolean isSymmetric(TreeNode root) {
if (root == null){
return true;
}
if (root.left == null && root.right == null){
return true;
}
return isMirror(root.left,root.right);
}
private boolean isMirror(TreeNode left,TreeNode right){
if (left == null && right == null){
return true;
}
if (left == null || right == null){
return false;
}
if (left.val != right.val){
return false;
}
//判断left的左树和right的右树是否相等 && left的右树和right的左树是否相等
return isMirror(left.left,right.right) && isMirror(left.right,right.left);
}
}