Leetcode-Symmetric Tree——判断二叉树是否对称

作者:disappearedgod
文章出处:http://blog.csdn.net/disappearedgod/article/details/24153001
时间:2014-5-14

题目

Symmetric Tree

  Total Accepted: 12814  Total Submissions: 40358 My Submissions

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
    \
     5
The 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 q1 = new LinkedList();
       Queue q2 = new LinkedList();
       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 l = new LinkedList(),
                            r = new LinkedList();
        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-Same Tree(java)
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;
    }



返回

LeetCode Solution(持续更新,java>c++)



你可能感兴趣的:(LeetCode,树)