剑指offer58 对称的二叉树

题目

请实现一个函数,用来判断一棵二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。

思路

1.第一种暴力方法,直接把根的右子树全部镜像,然后跟根的左子树对比

    递归:递归函数功能,递归停止条件,递归下一步

    注意先判断root是否为0,再获取其left或者right,否则就有空指针问题

2.用队列

代码

/*
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;
        Mirror(pRoot.right);
        return isMirror(pRoot.left,pRoot.right);
        
        
    }
    
    void Mirror(TreeNode root)
    {
        if(root == null)
            return;
        if(root.left == null && root.right == null)
            return;
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        Mirror(root.left);
        Mirror(root.right);
    }
    
    boolean isMirror(TreeNode root1,TreeNode root2)
    {
        if(root1 == null && root2 != null)
            return false;
        if(root1 != null && root2 == null)
            return false;
        if(root1 == null && root2==null)
            return true;
        if(root1.val != root2.val)
            return false;
        if(root1.left == root2.left && root1.left == null && root1.right == root2.right && root1.right == null)
            return true;
        return isMirror(root1.left,root2.left)&&isMirror(root1.right,root2.right);
    }
}
import java.util.LinkedList;
import java.util.Queue;
/*
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 || (pRoot.left == null && pRoot.right == null))
            return true;
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        if(pRoot.left != null && pRoot.right != null)
        {
            queue.add(pRoot.left);
            queue.add(pRoot.right);
        }
        else
            return false;
        while(!queue.isEmpty())
        {
            TreeNode n1 = queue.remove();
            TreeNode n2 = queue.remove();
            if(n1.left != null && n2.right == null)
                return false;
            if(n1.left == null && n2.right != null)
                return false;
            if(n1.val != n2.val)
                return false;
            if(n1.left != null && n2.right != null)
            {
                queue.add(n1.left);
                queue.add(n2.right);
            }
            if(n1.right != null && n2.left != null)
            {
                queue.add(n1.right);
                queue.add(n2.left);
            }
            
        }
        return true;
        
    }
    
}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值