思路:
1.递归,遍历左子树和右子树对比
2.终止条件,如果当前两个要遍历的节点为空。递归条件,当前两个节点的值相等,就继续递归下去。
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
boolean isSymmetrical(TreeNode pRoot) {
if(pRoot == null){
return true;
}
return isSame(pRoot.left, pRoot.right);
}
boolean isSame(TreeNode node1,TreeNode node2){
if(node1 == null && node2 == null){
return true;
}
if(node1 == null || node2 == null){
return false;
}
if(node1.val == node2.val){
return isSame(node1.left, node2.right) && isSame(node1.right, node2.left);
}
return false;
}
}