作者:disappearedgod
时间:2014-5-14
题目
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
Note:
Bonus points if you could solve it both recursively and iteratively.
confused what "{1,#,2,3}"
means? > read more on how binary tree is serialized on OJ.
OJ's Binary Tree Serialization:
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
1 / \ 2 3 / 4 \ 5The above binary tree is serialized as
"{1,2,3,#,#,4,#,#,5}"
.
解法
迭代
public boolean isSymmetric(TreeNode root) {
if(root==null)
return true;
return ST(root.left,root.right);
}
private boolean ST(TreeNode left, TreeNode right){
TreeNode l = left;
TreeNode r = right;
if(l==null && r == null)
return true;
else if(l!=null && r != null){
return l.val==r.val && ST(l.left,r.right) &&ST(l.right,r.left);
}
else return false;
}
队列
public boolean isSymmetric(TreeNode root) {
if(root==null||(root.left==null && root.right ==null))
return true;
//return ST(root.left,root.right);
return ST1(root);
}
private boolean ST1(TreeNode root){
Queue<TreeNode> q1 = new LinkedList<TreeNode>();
Queue<TreeNode> q2 = new LinkedList<TreeNode>();
q1.offer(root.left);
q2.offer(root.right);
while(!q1.isEmpty() && !q2.isEmpty()){
TreeNode p = q1.poll();
TreeNode q = q2.poll();
if(p==null){
if(q!=null)
return false;
else
continue;
}
if(q==null|| p.val!=q.val)
return false;
q1.offer(p.left);
q2.offer(q.right);
q2.offer(p.right);
q1.offer(q.left);
}
return true;
}
堆栈
public boolean isSymmetric(TreeNode root) {
if(root==null||(root.left==null && root.right ==null))
return true;
//return ST(root.left,root.right);
return ST2(root);
}
public boolean ST2(TreeNode root) {
// Start typing your Java solution below
// DO NOT write main() function
if(root==null) return true;
LinkedList<TreeNode> l = new LinkedList<TreeNode>(),
r = new LinkedList<TreeNode>();
l.add(root.left);
r.add(root.right);
while(!l.isEmpty() && !r.isEmpty()){
TreeNode temp1=l.poll(),
temp2=r.poll();
if(temp1==null && temp2!=null || temp1!=null && temp2==null)
return false;
if(temp1!=null){
if(temp1.val!=temp2.val) return false;
l.add(temp1.left);
l.add(temp1.right);
r.add(temp2.right);
r.add(temp2.left);
}
}
return true;
}
相关习题
Leetcode-Symmetric Tree
综合
其实对于这道题有一个简单的想法:就是如果我们把任意一颗树镜像对折后,看处理完的树是否与原来的树相同,如果相同就是对称树。
想法虽然简单但是需要三步:
首先,需要克隆这棵树。
然后,将克隆的树与这棵树进行比对。
最后我们来比对这两棵树,如果相同则是对称树。
代码如下
public boolean isSymmetric(TreeNode root) {
if(root==null||(root.left==null && root.right ==null))
return true;
//return ST(root.left,root.right);
return ST3(root);
}
public boolean ST3(TreeNode root){
if(root==null)
return true;
TreeNode root1= clone(root);
MirrorRecursively(root1);
return isSameTree(root,root1);
}
public TreeNode clone(TreeNode root){
if(root==null||(root.left ==null && root.right == null))
return root;
TreeNode cloneTree = new TreeNode(root.val);
cloneTree.left = clone(root.left);
cloneTree.right = clone(root.right);
return cloneTree;
}
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p!=null&&q!=null){
if(p.val==q.val )
return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
else
return false;
}
else if(p==null&&q==null)
return true;
else
return false;
}
public void MirrorRecursively(TreeNode root){
if(root == null||(root.left ==null&& root.right == null))
return ;
TreeNode temp = root.right;
root.right = root. left;
root.left = temp;
if(root.left!=null)
MirrorRecursively(root.left);
if(root.right!=null)
MirrorRecursively(root.right);
}
改进方法
public boolean isSymmetric(TreeNode root) {
if(root==null||(root.left==null && root.right ==null))
return true;
//return ST(root.left,root.right);
return ST3(root);
}
public boolean ST3(TreeNode root){
if(root==null)
return true;
TreeNode root1= MirrorRecursively(root);
return isSameTree(root,root1);
}
public TreeNode MirrorRecursively(TreeNode root){
if(root==null||(root.left ==null && root.right == null))
return root;
TreeNode MirrorTree = new TreeNode(root.val);
MirrorTree.left = MirrorRecursively(root.right);
MirrorTree.right = MirrorRecursively(root.left);
return MirrorTree;
}
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p!=null&&q!=null){
if(p.val==q.val )
return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
else
return false;
}
else if(p==null&&q==null)
return true;
else
return false;
}