# 【LeetCode】101. Symmetric Tree 解题报告

## Subject

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree is symmetric:

    1
/ \
2   2
/ \ / \
3  4 4  3

But the following is not:

    1
/ \
2   2
\   \
3    3

## Solution

### solution 1

    /**
* 双端队列 <br />
* DFS <br />
* 3ms <br />
* beats 6.29% of java submissions
*
* @param root
* @return
*/
public boolean isSymmetric(TreeNode root) {
if (root == null) {
return true;
}

TreeNode preNode = null;
TreeNode postNode = null;

while (!deque.isEmpty()) {
preNode = deque.pollFirst();
postNode = deque.pollLast();
if (preNode == null && postNode == null) {
continue;
}
if (preNode == null || postNode == null) {
return false;
}
if (preNode.val != postNode.val) {
return false;
} else {

}
}
return true;
}

### solution 2

    /**
* 递归方式 <br />
* 1ms <br />
*
* @param root
* @return
*/
public boolean isSysmmetric2(TreeNode root) {
if (root == null) {
return true;
}

return checkNodes(root.left, root.right);
}

public boolean checkNodes(TreeNode node1, TreeNode node2) {
if (node1 == null && node2 == null) {
return true;
}
if (node1 == null || node2 == null) {
return false;
}
if (node1.val != node2.val) {
return false;
} else {
return checkNodes(node1.left, node2.right)
&& checkNodes(node1.right, node2.left);
}
}

1ms

### solution 3

    /**
* 拷贝一颗二叉树，reverse。或者拷贝的时候直接反转。 <br />
* 然后在使用Same Tree的方法判断这两个树是否一样。 <br />
* 1ms
*
* @param root
* @return
*/
public boolean isSysmmetric3(TreeNode root) {
if (root == null) {
return true;
}

TreeNode newRootNode = copyNode(root);

SameTree sameTree = new SameTree();
return sameTree.isSameTree(root, newRootNode);
}

/**
* 左右对换结点拷贝二叉树
*
* @param node
* @return
*/
private TreeNode copyNode(TreeNode node) {
if (node == null) {
return null;
}
TreeNode treeNode = new TreeNode(node.val);
treeNode.left = copyNode(node.right);
treeNode.right = copyNode(node.left);
return treeNode;
}

【Reverse Binary Tree】

bingo~~