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

34 篇文章 0 订阅
作者:disappearedgod
时间: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<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;
    }





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值