symmetric-tree

1、链接:symmetric-tree来源:牛客网

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 

2、代码:

import java.util.LinkedList;
import java.util.Queue;

/**
 * 思路:(树的结点值都为个位数)
          1
         / \
        3   3
       / \ / \
      4  5 5  4
     /\ /\ /\ /\
    9 7 62 26 7 9
  ...
 假如树在一张纸上,对折纸后结点重合,重合处值相等,满足这样的树就是如题所说的“对称”。
 以上图为例:
  以 左3 作为根结点做前序遍历(根、左、右)依次遍历是 
                3497562 (这些元素都在1为根节点左子树)
  以 右3 作为根节点做“对称前序遍历”(根、右、左)依次遍历是
                3497562 (这些元素都在1为根节点右子树)
    可以看出如果对称的话 前序遍历 和 “对称前序遍历”(根、右、左)的结果是一样的
    (如果单单是先序遍历,然后对比该结点是不够的,因为先序遍历确定后,树的形状也可能是不同,故而还需要比较当前结点的左右结点情况)
从而也可以利用
    中序遍历(左、根、右)和“对称中序遍历”(右、根、左)
 或      
    后序遍历(左、右、根)和“对称后序遍历”(右、左、根)
来解这道题
 * @author Rail
 *
 */
public class IsSymmetric {
    boolean isSame = true;
    public boolean isSymmetric(TreeNode root) {
        if(root == null)
            return true;
        reverse(root.left, root.right);

        return isSame;
    }
    private void reverse(TreeNode left, TreeNode right) {
        if(left == null && right == null)
            return ;
        if((left != null && right == null) || (left == null && right != null)
                || (left.val != right.val)){
            isSame = false;
            return ;
        }else{
            if(isSame){//避免在已经不相同的情况还继续遍历
                reverse(left.left, right.right);
                reverse(left.right, right.left);
            }
        }
    }
    /*非递归
     * public boolean isSymmetric(TreeNode root) {
        if(root == null)
            return true;

        boolean isMirror = true;
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        if(root.left == null || root.right == null){
            return root.left == root.right;
        }
        queue.add(root.left);
        queue.add(root.right);

        while(!queue.isEmpty()){
            int size = queue.size();
            while(size > 0){
                TreeNode left = queue.poll();
                TreeNode right = queue.poll();
                size -= 2;

                if((left == null || right == null) && left != right)//不能删除里面的括号,&&优先级高于||
                    return false;
                if(left == null || right == null)
                    continue;
                if(left.val != right.val)
                    return false;

                queue.add(left.left);
                queue.add(right.right);
                queue.add(left.right);
                queue.add(right.left);
            }
        }

        return isMirror;
    }*/
}   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值