# Leetcode - Symmetric Tree

Question:

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

Paste_Image.png

But the following is not:

Paste_Image.png
Paste_Image.png

My code:

``````/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null)
return true;
else
return isSymmetric(root.left, root.right);
}

private boolean isSymmetric(TreeNode left, TreeNode right) {
if (left == null && right == null)
return true;
else if (left != null && right == null)
return false;
else if (left == null && right != null)
return false;
else {
return (left.val == right.val) && isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left);

}
}
}

``````

My test result:

Paste_Image.png

**

**

Good luck, Richardo!

My code:

``````/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null)
return true;
q.offer(root);
while (!q.isEmpty()) {
int size = q.size();
ArrayList<TreeNode> l = new ArrayList<TreeNode>(size);
while (!q.isEmpty())
for (int i = 0; i < size / 2; i++) {
TreeNode left = l.get(i);
TreeNode right = l.get(size - i - 1);
if (left.val != right.val)
return false;
if (left.left == null && right.right != null)
return false;
if (left.left != null && right.right == null)
return false;
if (left.right == null && right.left != null)
return false;
if (left.right != null && right.left == null)
return false;
}
for (int i = 0; i < size; i++) {
TreeNode temp = l.get(i);
if (temp.left != null)
q.offer(temp.left);
if (temp.right != null)
q.offer(temp.right);
}
if (q.size() % 2 == 1)
return false;
}
return true;
}
}
``````

My code:

``````/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null)
return true;
else if (root.left == null && root.right == null)
return true;
else if (root.left == null || root.right == null)
return false;
return isSymmetric(root.left, root.right);
}

private boolean isSymmetric(TreeNode left, TreeNode right) {
if (left.val != right.val)
return false;
boolean b1 = false;
boolean b2 = false;
if (left.left == null && right.right == null)
b1 = true;
else if (left.left == null || right.right == null)
return false;
else
b1 = isSymmetric(left.left, right.right);
if (!b1)
return false;

if (left.right == null && right.left == null)
b2 = true;
else if (left.right == null || right.left == null)
return false;
else
b2 = isSymmetric(left.right, right.left);

return b2;
}
}
``````

Anyway, Good luck, Richardo!

My code:

``````/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null) {
return true;
}
return helper(root.left, root.right);
}

private boolean helper(TreeNode left, TreeNode right) {
if (left == null && right == null) {
return true;
}
else if (left == null || right == null) {
return false;
}
else if (left.val != right.val) {
return false;
}
else {
return helper(left.left, right.right) && helper(left.right, right.left);
}
}
}
``````

recursion 不是很难。

My code:

``````/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSymmetric(TreeNode root) {
while (!q.isEmpty()) {
TreeNode n1 = q.poll();
TreeNode n2 = q.poll();
if (n1 == null && n2 == null) {
continue;
}
else if (n1 == null || n2 == null) {
return false;
}
else if (n1.val != n2.val) {
return false;
}
else {
q.offer(n1.left);
q.offer(n2.right);
q.offer(n1.right);
q.offer(n2.left);
}
}

return true;
}
}
``````

Interesting

Anyway, Good luck, Richardo! — 08/28/2016

原文作者：Richardo92