LeetCode Top 100 Liked Questions 101. Symmetric Tree (Java版; Easy)

welcome to my blog

LeetCode Top 100 Liked Questions 101. Symmetric Tree (Java版; Easy)

题目描述
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

    1
   / \
  2   2
 / \ / \
3  4 4  3


But the following [1,2,2,null,3,null,3] is not:

    1
   / \
  2   2
   \   \
   3    3
class Solution {
    public boolean isSymmetric(TreeNode root) {
        if(root==null){
            return true;
        }
        return core(root, root);
    }

    private boolean core(TreeNode root1, TreeNode root2){
        if(root1==null && root2==null){
            return true;
        }
        if(root1==null || root2==null){
            return false;
        }
        if(root1.val!=root2.val){
            return false;
        }
        return core(root1.left, root2.right) && core(root1.right, root2.left);
    }
}
第一次做, 迭代版, 创建队列, 镜像入队, 具体见注释; 要掌握下面的递归版
/*
BFS
创建队列
初始时, 将根节点入队两次
当队列不为空时
    每次出队两个元素curr1, curr2, 判断是否相等
    将curr1.left入队,curr2.right入队(这是镜像的入队方式)
    将curr1.right入队, curr2.left入队(这是镜像的入队方式)
*/
import java.util.LinkedList;

class Solution {
    public boolean isSymmetric(TreeNode root) {
        if(root==null)
            return true;
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        queue.add(root);
        TreeNode curr1, curr2;
        while(!queue.isEmpty()){
            curr1 = queue.poll();
            curr2 = queue.poll();
            if(curr1 == null && curr2 ==null)
                continue;
            if(curr1==null || curr2==null)
                return false;
            //here, curr1!=null && curr2!=null
            if(curr1.val!=curr2.val)
                return false;
            //镜像入队
            queue.add(curr1.left);
            queue.add(curr2.right);
            
            queue.add(curr1.right);
            queue.add(curr2.left);
        }
        return true;
    }
}   
第一次做, 在一个递归函数同时进行两种遍历:左根右, 右根左; 要弄清楚为什么一个递归函数可以同时实现两种遍历, 很明显我们无法做到在在一个递归函数中同时实现前序遍历和中续遍历, 那为什么能够在同一个递归函数中实现左根右,右根左这两种遍历方式呢? 核心是因为左根右和右根左这两种遍历方式调用递归函数的位置是相同的
/*
同时进行两种遍历: 左根右, 右根左; 检查对应的值对否相等; 
脑中偶尔会闪现出一种疑惑, 如何在一个递归函数中同时实现两种遍历:左根右, 右根左? 
因为我觉得不同的遍历需要用不同的递归函数, 以递归版的前序遍历为例, 需要对root.left重新调用一个递归函数; 所以说对于新条件(新节点), 需要重新调用两个递归函数, 这就是我疑惑的来源
但事实上, 左根右, 右根左这两种遍历非常的相似, 处理的都是相同层上的节点, 所以能够写在一个递归函数中
*/
class Solution {
    public boolean isSymmetric(TreeNode root) {
        if(root==null)
            return true;
        boolean res = Core(root.left, root.right);
        return res;
    }
    public boolean Core(TreeNode node1, TreeNode node2){
        if((node1==null&&node2!=null) || (node1!=null&&node2==null))
            return false;
        if(node1==null && node2==null)
            return true;
        //here, node1!=null && node2!=null
        if(node1.val != node2.val)
            return false;
        return Core(node1.left, node2.right) && Core(node1.right, node2.left);   
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值