对称二叉树
题目描述
给你一个二叉树的根节点 root , 检查它是否轴对称。
示例
Java代码
class Solution {
public boolean isSymmetric(TreeNode root) {
return doCompare(root.left, root.right);
}
private boolean doCompare(TreeNode tree1, TreeNode tree2) {
if(tree1 == null && tree2 == null) {
return true;
}
if(tree1 == null || tree2 == null || tree1.val != tree2.val) {
return false;
}
return doCompare(tree1.left, tree2.right) && doCompare(tree1.right, tree2.left);
}
}