# 剑指offer第二版-28.对称的二叉树

``````package structure;
import java.util.Queue;
/**
* Created by ryder on 2017/6/12.
* 树节点
*/
public class TreeNode<T> {
public T val;
public TreeNode<T> left;
public TreeNode<T> right;
public TreeNode(T val){
this.val = val;
this.left = null;
this.right = null;
}
//层序遍历
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder("[");
queue.offer(this);
TreeNode<T> temp;
while(!queue.isEmpty()){
temp = queue.poll();
stringBuilder.append(temp.val);
stringBuilder.append(",");
if(temp.left!=null)
queue.offer(temp.left);
if(temp.right!=null)
queue.offer(temp.right);
}
stringBuilder.deleteCharAt(stringBuilder.lastIndexOf(","));
stringBuilder.append("]");
return stringBuilder.toString();
}
}
``````
``````package chapter4;
import structure.TreeNode;
import java.util.Queue;
/**
* Created by ryder on 2017/7/15.
* 对称的二叉树
*/
public class P159_SymmetricalBinaryTree {
//递归实现
public static boolean isSymmetrical(TreeNode<Integer> root){
if(root==null)
return true;
if(root.left==null && root.right==null)
return true;
if(root.left==null || root.right==null)
return false;
return isSymmetrical(root.left,root.right);
}
public static boolean isSymmetrical(TreeNode<Integer> root1,TreeNode<Integer> root2){
if(root1==null && root2==null)
return true;
if(root1==null || root2==null)
return false;
if(!root1.val.equals(root2.val))
return false;
return isSymmetrical(root1.left,root2.right) && isSymmetrical(root1.right,root2.left);
}
//迭代实现
public static boolean isSymmetrical2(TreeNode<Integer> root){
if(root==null)
return true;
if(root.left==null && root.right==null)
return true;
if(root.left==null || root.right==null)
return false;
queueLeft.offer(root.left);
queueRight.offer(root.right);
TreeNode<Integer> tempLeft,tempRight;
while(!queueLeft.isEmpty()|| !queueRight.isEmpty()){
tempLeft = queueLeft.poll();
tempRight = queueRight.poll();
if(tempLeft.val.equals(tempRight.val)){
if(tempLeft.left!=null)
queueLeft.offer(tempLeft.left);
if(tempLeft.right!=null)
queueLeft.offer(tempLeft.right);
if(tempRight.right!=null)
queueRight.offer(tempRight.right);
if(tempRight.left!=null)
queueRight.offer(tempRight.left);
}
else
return false;
}
if(queueLeft.isEmpty() && queueRight.isEmpty())
return true;
else
return false;
}
public static void main(String[] args){
TreeNode<Integer> root = new TreeNode<>(8);
root.left = new TreeNode<>(6);
root.right = new TreeNode<>(6);
root.left.left = new TreeNode<>(5);
root.left.right = new TreeNode<>(7);
root.right.left = new TreeNode<>(7);
root.right.right = new TreeNode<>(5);
System.out.println(isSymmetrical(root));
System.out.println(isSymmetrical2(root));
}
}
``````

``````true
true
``````

### 推荐阅读更多精彩内容

• 面试题7：重建二叉树 题目： 输入某二叉树的前序遍历和中序遍历的结果。请重建该二叉树。假设输入的前序遍历和中序遍历...
lyoungzzz阅读 172评论 0 0
• 树的概述 树是一种非常常用的数据结构，树与前面介绍的线性表，栈，队列等线性结构不同，树是一种非线性结构 1.树的定...
Jack921阅读 3,247评论 1 30
• 数据结构和算法--二叉树的实现 几种二叉树 1、二叉树 和普通的树相比，二叉树有如下特点： 每个结点最多只有两棵子...
sunhaiyu阅读 3,575评论 0 13
• 四、树与二叉树 1. 二叉树的顺序存储结构 二叉树的顺序存储就是用数组存储二叉树。二叉树的每个结点在顺序存储中都有...
MinoyJet阅读 777评论 0 7
• 宝宝应该从小养成吃早餐的好习惯，吃好早餐可以让宝宝一整天都活力充沛、精神饱满。但日常生活中，很多妈妈给宝宝准备的早...
优特宝贝阅读 384评论 0 4