一、题目
二、代码
/**
* 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)
{
Queue<TreeNode> re = new LinkedList<>();
re.offer(root);
while(!re.isEmpty())
{
// System.out.println("层数加一 ");
int i;
int length = re.size();
int[] judge = new int[2*length];
for(i=0;i<length;i++)
{
TreeNode temp_node = re.poll();
if(temp_node.left!=null)
{
re.offer(temp_node.left);
judge[i*2] = temp_node.left.val;
}
else
{
judge[i*2] = 101;
}
if(temp_node.right!=null)
{
re.offer(temp_node.right);
judge[i*2+1] = temp_node.right.val;
}
else
{
judge[i*2+1] = 101;
}
}
for(i=0;i<length;i++)
{
// System.out.println("i "+ i);
if(judge[i]!=judge[2*length-1-i]) return false;
}
}
return true;
}
}