题目描述:请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
思路一:递归
首先根节点以及其左右子树,左子树的左子树和右子树的右子树相同,左子树的右子树和右子树的左子树相同即可。
public class Solution {
boolean isSymmetrical(TreeNode pRoot)
{
if (pRoot == null)
return true;
return comRoot(pRoot.left, pRoot.right);
}
boolean comRoot(TreeNode pLeft, TreeNode pRight)
{
if (pLeft == null) return pRight == null;
if (pRight == null) return false;
if (pLeft.val != pRight.val) return false;
return comRoot(pLeft.left, pRight.right) && comRoot(pLeft.right, pRight.left);
}
}
思路二:递归
采用递归的中序遍历(左中右)及反中序遍历(右中左)
import java.util.ArrayList;
public class Solution {
ArrayList<Integer> list = new ArrayList<>();
ArrayList<Integer> listReverse = new ArrayList<>();
boolean isSymmetrical(TreeNode pRoot)
{
inorder(pRoot);
inorderReverse(pRoot);
for (int i = 0; i < list.size(); i++)
if (list.get(i) != listReverse.get(i))
return false;
return true;
}
private void inorder(TreeNode pRoot)
{
if (pRoot == null) {
list.add(-1);
return;
}
if (pRoot.left == null && pRoot.right == null)
{
list.add(pRoot.val);
return;
}
inorder(pRoot.left);
list.add(pRoot.val);
inorder(pRoot.right);
}
private void inorderReverse(TreeNode pRoot) {
if (pRoot == null) {
listReverse.add(-1);
return;
}
if (pRoot.left == null && pRoot.right == null) {
listReverse.add(pRoot.val);
return;
}
inorderReverse(pRoot.right);
listReverse.add(pRoot.val);
inorderReverse(pRoot.left);
}
}
思路三:非递归
利用非递归的中序遍历(左中右)及反中序遍历(右中左)
import java.util.LinkedList;
public class Solution {
boolean isSymmetrical(TreeNode pRoot)
{
if (pRoot == null)
return true;
if (pRoot.left == null || pRoot.right == null)
{
if (pRoot.left == null && pRoot.right == null)
return true;
return false;
}
TreeNode left = pRoot.left;
TreeNode right = pRoot.right;
LinkedList<TreeNode> q1 = new LinkedList<>();
LinkedList<TreeNode> q2 = new LinkedList<>();
while ((left != null && right != null) || (!q1.isEmpty() && !q2.isEmpty()))
{
while (left != null && right != null)
{
if (left.val != right.val)
return false;
q1.add(left);
q2.add(right);
left = left.left;
right = right.right;
}
if (left == null && right == null)
{
if (!q1.isEmpty() && !q2.isEmpty()) {
left = q1.poll();
left = left.right;
right = q2.poll();
right = right.left;
}
}
else return false;
}
if (left == null && right == null)
return true;
else
return false;
}
}
思路四:非递归
思路三的变形
import java.util.LinkedList;
public class Solution {
boolean isSymmetrical(TreeNode pRoot)
{
if (pRoot == null)
return true;
if (pRoot.left == null || pRoot.right == null)
{
if (pRoot.left == null && pRoot.right == null)
return true;
return false;
}
TreeNode left = pRoot.left;
TreeNode right = pRoot.right;
LinkedList<TreeNode> q1 = new LinkedList<>();
LinkedList<TreeNode> q2 = new LinkedList<>();
q1.add(left);
q2.add(right);
while (!q1.isEmpty())
{
left = q1.poll();
right = q2.poll();
if (left == null && right == null) continue;
if ((left == null && right != null) || (left != null && right == null)) return false;
if (left.val != right.val) return false;
q1.add(left.left);
q1.add(left.right);
q2.add(right.right);
q2.add(right.left);
}
return true;
}
}