101. 对称二叉树(java)
题目描述:
给你一个二叉树的根节点 root
, 检查它是否轴对称。
示例 1:
输入:root = [1,2,2,3,4,4,3] 输出:true示例 2:
输入:root = [1,2,2,null,3,null,3] 输出:false
代码思路:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null){
return true;
}
if(judge(root.left,root.right)>0){
return false;
}
return true;
}
public int judge(TreeNode lx,TreeNode rx){
if(lx==null&&rx==null){
return 0;
}
if(lx==null&&rx!=null || lx!=null&&rx==null){
return 1;
}
if(lx.val !=rx.val){
return 1;
}
int m = 0;
m=m+judge(lx.left,rx.right)+judge(lx.right,rx.left);
return m;
}
}