题目地址 https://leetcode-cn.com/problems/symmetric-tree/

1.递归

本题最简单的思路是递归,可以假设两棵一模一样的树在进行镜像对比。他们之间的关系满足node1.left == node2.rightnode1.right == node2.left

时间复杂度O(n) n为节点的个数;空间复杂度O(h) h为二叉树的最大深度

class Solution {
public boolean isSymmetric(TreeNode root) {
return mirror(root, root);
}
private boolean mirror(TreeNode node1, TreeNode node2) {
if (node1 == null && node2 == null) return true;
if (node1 == null || node2 == null) return false;
return node1.val == node2.val
&& mirror(node1.left, node2.right)
&& mirror(node2.left, node1.right);
}
}

2.BFS

广度优先搜索思路还是和递归一样,假设是两棵一模一样的树在进行镜像对比。时间复杂度O(n) 空间复杂度O(n)

public boolean isSymmetric(TreeNode root) {
Queue<TreeNode> q = new LinkedList<>();
q.add(root);
q.add(root);
while (!q.isEmpty()) {
TreeNode t1 = q.poll();
TreeNode t2 = q.poll();
if (t1 == null && t2 == null) continue;
if (t1 == null || t2 == null) return false;
if (t1.val != t2.val) return false;
q.add(t1.left);
q.add(t2.right);
q.add(t1.right);
q.add(t2.left);
}
return true;
}

java版本

class Solution {
public boolean isSymmetric(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
if (root == null)
return true;
queue.offer(root);
queue.offer(root);
while(!queue.isEmpty()) {
TreeNode node1 = queue.poll();
TreeNode node2 = queue.poll();
if (node1 == null && node2 == null)
continue;
if (node1 == null || node2 == null)
return false;
if (node1.val != node2.val)
return false;
queue.offer(node1.left);
queue.offer(node2.right);
queue.offer(node1.right);
queue.offer(node2.left);
}
return true;
}
}

更多LeetCode题解和数据结构方面的内容,可以关注我的github,求个star~ ▄█▔▉●

leetcode-0101 对称二叉树的更多相关文章

  1. LeetCode 101 对称二叉树的几种思路(Python实现)

    对称二叉树 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的.   1   / \ 2   2 / \ / \3 4 4 3 但是下面这个 [1,2,2 ...

  2. Java实现 LeetCode 101 对称二叉树

    101. 对称二叉树 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2 ...

  3. 领扣(LeetCode)对称二叉树 个人题解

    给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3,nul ...

  4. LeetCode 101. 对称二叉树(Symmetric Tree)

    题目描述 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null, ...

  5. LeetCode 101.对称二叉树 - JavaScript

    题目描述:给定一个二叉树,检查它是否是镜像对称的. 题目分析 下面这种二叉树就是镜像对称的,符合题目要求: 1 / \ 2 2 / \ / \ 3 4 4 3 解法 1:递归检查 根据题目" ...

  6. 【Leetcode】对称二叉树

    递归法 执行用时 :12 ms, 在所有 C++ 提交中击败了43.44%的用户 内存消耗 :14.6 MB, 在所有 C++ 提交中击败了95.56%的用户 /** * Definition for ...

  7. LeetCode【101. 对称二叉树】

    对称二叉树,就是左节点的左节点等于右节点的右节点,左节点的右节点等于右节点的左节点. 很自然就想到迭代与递归,可以创建一个新的函数,就是另一个函数不断的判断,返回在主函数. class Solutio ...

  8. Leecode刷题之旅-C语言/python-101对称二叉树

    /* * @lc app=leetcode.cn id=101 lang=c * * [101] 对称二叉树 * * https://leetcode-cn.com/problems/symmetri ...

  9. C语言递归之对称二叉树

    题目描述 给定一个二叉树,检查它是否是镜像对称的. 示例 二叉树 [1,2,2,3,4,4,3] 是对称的. / \ / \ / \ [1,2,2,null,3,null,3] 则不是镜像对称的. / ...

  10. 【Leetcode】104. 二叉树的最大深度

    题目 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例:给定二叉树 [3,9,20,null,null,15,7 ...

随机推荐

  1. JavaScript语法记要

    JavaScript语法记要 1.JS代码忽略缩进和换行 2.JS六种数据类型 String // 字符串 Number // 数值 Boolean // 布尔值 null // 空值 undefin ...

  2. 【原创】Linux select/poll机制原理分析

    前言 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 1. 概述 Linux系统 ...

  3. coding++:Spring中的@Transactional(rollbackFor = Exception.class)属性详解

    异常: 如下图所示,我们都知道 Exception 分为 运行时异常 RuntimeException 和 非运行时异常. error 是一定会回滚的. 如果不对运行时异常进行处理,那么出现运行时异常 ...

  4. C# Protobuf如何做到0分配内存的序列化

    题目很简单, 就是IMessage对象怎么变成Byte[] 答案1: msg.ToByteArray() 这肯定不符合我们的要求 答案2: using var memoryStream = new M ...

  5. CSS躬行记(2)——伪类和伪元素

    一.伪类选择器 伪选择器弥补了常规选择器的不足,能够实现一些特殊情况下的样式,例如在鼠标悬停时或只给字符串中的第一个字符指定样式.与类选择器类似,可以从HTML元素的class属性中查看到,但伪选择器 ...

  6. 如何使用Rancher在OpenStack上创建K8S集群

    不可否认的是,OpenStack仍然是可行的云操作系统,并且被全世界许多互联服务提供商使用.而Rancher是业界最为广泛使用的Kubernetes管理平台,通过简洁直观的GUI集中管理企业IT中的多 ...

  7. C++中的字符串切片操作

    string str = "hello"; str.substr(0,2); //输出"he", 表示[0,2)

  8. Win 10 C 盘突然爆满,怎么清理?

    Win 10 C 盘突然爆满,怎么清理? 使用windows的小伙伴们都知道,C盘是安装系统的,有时候不知道为啥突然就爆满了,查看我的电脑,C盘显示红色的.是不是感觉狠揪心,想删除一些东西有不敢删除, ...

  9. (js描述的)数据结构[栈结构](2)

    (js描述的)数据结构[栈结构](2) 一.什么是栈结构 1.一种受限制的线性结构,这种结构可以基于数组来实现. 2.可以抽象成一个容器,上面的是栈顶,底下的是栈底.所以仅允许对栈顶进行操作, 二.栈 ...

  10. mappedBy和JoinColumn,onetomany。

    无论是onetomany,还是manytoone.都要设置级联关系(cascade),否则不会储存关联的数据. @Entity public class Clazzss { @Id @Generate ...