【Hot100】LeetCode—101. 对称二叉树

目录

  • [1- 思路](#1- 思路)
  • [2- 实现](#2- 实现)
    • [⭐101. 对称二叉树------题解思路](#⭐101. 对称二叉树——题解思路)
  • [3- ACM 实现](#3- ACM 实现)

1- 思路

借助队列

  • 1- 创建队列Queue<TreeNode> queue,初始化加入 root.leftroot.right
  • 2- 判断逻辑while(!queue.isEmpty())
    • 2-1 弹出两个结点: 分别为 lr
      • ① 情况1:l == nullr ==null 此时,直接 continue
      • ② 情况2:l == nullr !=null 此时,直接 返回 false
      • ③ 情况3:l != nullr ==null 此时,直接 返回 false
      • ④ 情况4:l.val != r.val 直接返回 false
    • 2-2 入栈顺序
      • 先入栈 l.left ,再入栈 r.right
      • 先入栈 l.right,再入栈 r.left

2- 实现

⭐101. 对称二叉树------题解思路

java 复制代码
class Solution {
    public boolean isSymmetric(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();

        queue.offer(root.left);
        queue.offer(root.right);
        while(!queue.isEmpty()){
            TreeNode l = queue.poll();
            TreeNode r = queue.poll();

            // 四种
            if( l == null && r==null){
                continue;
            }
            if( l== null && r!=null ){
                return false;
            }
            if(l!=null && r == null){
                return false;
            }
            if(l.val!=r.val){
                return false;
            }
            queue.offer(l.left);
            queue.offer(r.right);
            queue.offer(l.right);
            queue.offer(r.left);
        }
        return true;
    }
}

3- ACM 实现

java 复制代码
public class isSymmetric {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public static TreeNode build(String str){
        if(str == null || str.length()==0){
            return null;
        }
        String input = str.replace("[","");
        input = input.replace("]","");
        String[] parts = input.split(",");

        Integer[] nums = new Integer[parts.length];
        for(int i = 0 ; i < parts.length;i++){
            if(!parts[i].equals("null")){
                nums[i] = Integer.parseInt(parts[i]);
            }else{
                nums[i] = null;
            }
        }
        Queue<TreeNode> queue = new LinkedList<>();
        TreeNode root = new TreeNode(nums[0]);
        queue.offer(root);
        int index = 1;
        while(!queue.isEmpty()&& index<parts.length){
            TreeNode node = queue.poll();
            if(index<nums.length && nums[index]!=null){
                node.left = new TreeNode(nums[index]);
                queue.offer(node.left);
            }
            index++;
            if(index<nums.length && nums[index]!=null){
                node.right = new TreeNode(nums[index]);
                queue.offer(node.right);
            }
            index++;
        }
        return root;
    }

    public static boolean isSymetric(TreeNode root){
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root.left);
        queue.offer(root.right);
        while(!queue.isEmpty()){
            TreeNode l = queue.poll();
            TreeNode r = queue.poll();
            if(l==null && r==null){
                continue;
            }
            if(l!=null && r==null){
                return false;
            }
            if(l==null && r!=null){
                return false;
            }
            if(l.val!=r.val){
                return false;
            }
            queue.offer(l.left);
            queue.offer(r.right);
            queue.offer(l.right);
            queue.offer(r.left);
        }
        return true;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        TreeNode root = build(input);

        System.out.println("结果是"+isSymetric(root));
    }
    
}
相关推荐
科大饭桶38 分钟前
数据结构自学Day5--链表知识总结
数据结构·算法·leetcode·链表·c
我爱C编程3 小时前
基于Qlearning强化学习的1DoF机械臂运动控制系统matlab仿真
算法
chao_7893 小时前
CSS表达式——下篇【selenium】
css·python·selenium·算法
chao_7893 小时前
Selenium 自动化实战技巧【selenium】
自动化测试·selenium·算法·自动化
YuTaoShao3 小时前
【LeetCode 热题 100】24. 两两交换链表中的节点——(解法一)迭代+哨兵
java·算法·leetcode·链表
怀旧,3 小时前
【数据结构】8. 二叉树
c语言·数据结构·算法
泛舟起晶浪3 小时前
相对成功与相对失败--dp
算法·动态规划·图论
地平线开发者4 小时前
地平线走进武汉理工,共建智能驾驶繁荣生态
算法·自动驾驶
IRevers4 小时前
【自动驾驶】经典LSS算法解析——深度估计
人工智能·python·深度学习·算法·机器学习·自动驾驶
前端拿破轮4 小时前
翻转字符串里的单词,难点不是翻转,而是正则表达式?💩💩💩
算法·leetcode·面试