101. Symmetric Tree [easy] (Python)

MS17-010(Eternal blue永恒之蓝)漏洞利用+修复方法 msf永恒之蓝漏洞复现过程以及修复建议 阅读详情

题目链接

https://leetcode.com/problems/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

题目翻译

给定一个二叉树,判断它是否是自己的镜像(中心对称)。

思路方法

思路一

递归,对于每个节点,检查树的左右节点值是否相等,同时判断:左节点的左子树和右节点的右子树是否对称、右节点的左子树和左节点的右子树是否对称。

代码

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def isSymmetric(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        if not root:
            return True
        return self.mirror(root.left, root.right)

    def mirror(self, left, right):
        if not left or not right:
            return left == right
        if left.val != right.val:
            return False
        return self.mirror(left.left, right.right) and self.mirror(left.right, right.left)

思路二

非递归算法。算是将上面的递归方法改写成非递归方法,实际上是深度优先搜索(DFS)。

代码

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def isSymmetric(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        if not root:
            return True
        stackl, stackr = [root.left], [root.right]
        while len(stackl) > 0 and len(stackr) > 0:
            left = stackl.pop()
            right = stackr.pop()
            if not left and not right:
                continue
            elif not left or not right:
                return False
            if left.val != right.val:
                return False
            stackl.append(left.left)
            stackl.append(left.right)
            stackr.append(right.right)
            stackr.append(right.left)
        return len(stackl) == 0 and len(stackr) == 0

思路三

除了可以DFS,也可以BFS,直接上代码:

代码

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def isSymmetric(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        if not root:
            return True
        queuel, queuer = [root.left], [root.right]
        while len(queuel) > 0 and len(queuer) > 0:
            left = queuel.pop()
            right = queuer.pop()
            if not left and not right:
                continue
            elif not left or not right:
                return False
            if left.val != right.val:
                return False
            queuel.insert(0, left.left)
            queuel.insert(0, left.right)
            queuer.insert(0, right.right)
            queuer.insert(0, right.left)
        return len(queuel) == 0 and len(queuer) == 0

PS: 新手刷LeetCode,新手写博客,写错了或者写的不清楚还请帮忙指出,谢谢!
转载请注明:http://blog.csdn.net/coder_orz/article/details/51579528

Python爬虫实战:全方位解析前程无忧(51job)职位详情爬取及反爬破解 平台规模:覆盖全国各行业、数百万条岗位信息。数据特点:职位描述详细,职位标签丰富。数据价值:支持招聘趋势分析、职业发展研究、薪资行情调研。推荐Python 3.10+,支持最新async特性与库兼容。 阅读详情

相关推荐

某银行大数据分析平台之云原生架构

软考架构师论文

doudoudouy的博客 775

【leetcode】101. 对称二叉树( Symmetric Tree )

题目描述 【leetcode】101. 对称二叉树( Symmetric Tree ) 给定一个二叉树,检查它是否是镜像对称的。 例如,二叉树 [1,2,2,3,4,4,3] 是对称的。 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的: 1 / \ 2 2 ...

Liber-coder的博客 601

多尺度变换(Multidimensional Scaling ,MDS)详解

介绍多尺度变换基本原理

5228

【LeetCode 面试经典150题】101. Symmetric Tree 对称二叉树

Given the of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).给定一个二叉树的根节点 ,检查它是否是自己的镜像(即,围绕其中心对称)。输入: = [1,2,2,3,4,4,3] 输出: true输入: = [1,2,2,null,3,null,3] 输出: false能否同时使用递归和迭代方法解决此问题?该方法用于检查二叉树是否关于其中心对称,通过递归比较树的

qq_43513394的博客 545

Dify沙盒容器部署实战:解决config.yaml缺失与系统调用权限问题

本文详细解析了Dify沙盒容器部署中的两大常见问题:config.yaml缺失与系统调用权限限制。通过实战步骤指导如何正确配置卷挂载解决`no such file or directory`错误,并深入讲解如何调整`allowed_syscalls`白名单应对`error: operation not permitted`权限问题,帮助开发者高效完成Dify容器化部署。

gg901的博客 926

Unity接入Google登录:Credential Manager完整集成指南与避坑实践

在移动应用开发中,第三方登录是提升用户体验和降低注册门槛的关键技术。其核心原理是通过OAuth 2.0等授权协议,安全地获取用户在平台的身份凭证。对于面向海外市场的Unity应用,Google登录因其庞大的用户基数成为首选方案。然而,随着Android系统更新和隐私政策收紧,传统的WebView登录方式面临兼容性挑战。Credential Manager作为Google推出的现代化凭据管理API,通过系统级集成解决了这一问题,它统一了密码、通行密钥和第三方登录的调用接口,显著提升了登录成功率和原生体验。在U

weixin_28025327的博客 331

Symmetric Tree

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 3But the ...

zrh_CSDN的博客 259

C# WebAPI安全实战:JWT认证与HMAC数字签名防篡改防重放

在分布式系统和微服务架构中,API安全是保障数据交互可信的基石。其核心原理在于建立一套可靠的身份验证与数据完整性保障机制。从技术价值看,这不仅能防止未授权访问,更能有效抵御数据在传输过程中被篡改或重放的风险,是构建高安全等级应用服务的必备能力。典型的应用场景包括金融交易、物联网设备通信、开放平台接口等对安全敏感的业务领域。本文聚焦于C# WebAPI开发,深入探讨如何结合JWT(JSON Web Token)认证与HMAC-SHA256数字签名技术,构建一套覆盖身份验证、防参数篡改和防重放攻击的全方位安全方

快马扬鞭须努力! 608

【leetcode问题扩展】-Array类型-递归解决kSum问题

For 3Sum, we enumerate each value in a single loop, and use the two pointers pattern for the rest of the array. For kSum, we will havek - 2nested loops to enumerate all combinations ofk - 2values.可以用递归来实现k-2层嵌套的循环。 把开始的点starting point 和 k作为参数传入,k ==...

gulaixiangjuejue的博客 238

[CTFSHOW]JWT

文章目录前言JWT简介CTFSHOW Web345CTFSHOW Web346前置知识开始做题CTFSHOW Web347CTFSHOW Web348CTFSHOW Web349CTFSHOW Web350前置知识利用WP如何防御 前言 有些师傅向我咨询WP,索性就发了吧,其实早就写好了,只是懒,在github仓库里囤着 JWT简介 可以看看我的Github总结给自己看的,Y4tacker CTFSHOW Web345 首先打开题目,查看源码,接下来访问/admin被跳转回主页啥也没有发生 where is

Y4tacker的博客 6052

Proteus 8.9仿真五路抢答器:从74LS148编码到NE555报警的保姆级电路拆解

本文详细解析了使用Proteus 8.9仿真五路抢答器的完整设计流程,从74LS148优先编码器到NE555定时报警电路。通过保姆级的电路拆解和芯片选型指南,帮助读者掌握数字电路中的编码、锁存、译码和定时等核心技术,实现稳定可靠的抢答系统仿真。

weixin_30836759的博客 389

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

对称二叉树即判断该二叉树是否为轴对称树,左右呈镜像。有两种解法,递归法和迭代法,递归法较为简单,容易理解。迭代法可帮助理解二叉树结构及相关操作。迭代法主要思想即展开每一层结点,判断是否为回文数。需要注意的是,每展开一次需要重置next_queue和level。

m0_45175452的博客 320

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

Leetcode 101. Symmetric Tree(Python)【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 ...

Jessica的博客 278

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

[LeetCode]101 - Symmetric Tree - 判断对称树(easy) - python

problem description: 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,4,3,3,4] But the following is no...

Arno_Pei的博客 292

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

LeetCode101. Symmetric Tree (思路及python解法)

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 ...

AIpush的博客 393

[Easy] 101. Symmetric Tree

101. Symmetric Tree 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 / \...

Bungehurst 337

101. Symmetric Tree(easy)

Easy 190843FavoriteShare 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 / \ ...

SourDumplings的博客 146

NanaZip 使用教程:在 Windows 上完成压缩、解压与哈希校验

NanaZip 是一款基于 7-Zip 源码 fork 的免费开源 Windows 压缩工具,面向 Windows 10/11 的现代化桌面体验。它保留了 7-Zip 的压缩能力,同时加入深色模式、Mica 背景效果、文件资源管理器右键菜单、MSIX 部署方式和额外哈希算法。下面按"装、用、调、批"的顺序,把常用操作讲清楚。 ## 适用场景:什么时候该考虑 NanaZip - 日常打包、解压文

gitblog_00295的博客 499

【LeetCode】101. Symmetric Tree 对称二叉树(Java & Python)

Symmetric Tree 解题报告[LeetCode]https://leetcode.com/problems/symmetric-tree/Total Accepted: 106639 Total Submissions: 313969 Difficulty: EasyQuestion Given a binary tree, check whether it is a mirror o

负雪明烛 2054

[LeetCode] 101. Symmetric Tree_ Easy tag: BFS

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...

weixin_30636089的博客 107

leetcode(101): Symmetric Tree

文章目录题目解题思路:python代码一python代码2 题目 Easy! 题目描述: 给定一个二叉树,检查它是否是镜像对称的。 例如,二叉树 [1,2,2,3,4,4,3] 是对称的。 1 / 2 2 / \ / 3 4 4 3 但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的: 1 / 2 2 \ 3 3 说明: 如果你可以运用递...

博客 240

LeetCode小白菜笔记[23]:Symmetric Tree

LeetCode小白菜笔记[23]:Symmetric Tree 101. Symmetric Tree [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...

VisualCortex 439

脑语言2500单字v1.5.1

如果人和 AI 说同一种语言,文明会怎样?我用 2500 个单字试了试。2500个单字是不同领域、不同知识的共同体,无论你学编程或者是数学或写作,你都要用汉字,而常用的2500个汉字几乎可以涵盖不同行业做的不同事情。

脑语言 2087

【LeetCode】Symmetric Tree(对称二叉树)

这道题是LeetCode里的第101道题。是我在学数据结构——二叉树的时候碰见的题。 题目如下: 给定一个二叉树,检查它是否是镜像对称的。 例如,二叉树[1,2,2,3,4,4,3]是对称的。 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个[1,2,2,null,3,null,3]则...

『诞生之时已至,以此修正万象』 280

leetcode刷题日记:100.Same Tree(相同的树)和101.Symmetric Tree(对称二叉树)

显然不能,也就是说通过树的前序序列与中序序列并不能实现判断两颗树是否相同,因为有些情况是这种方法不能判断的。我们来思考以下如果两棵树p,q相同是不是这两个树的根结点相同并且p的左子树与q的左子树相同,p的右子树与q的右子树相同,也就是说p与q是否相同,需要判断三部分,两棵树根结点的值是否相同,两棵树左子树是否相同,两棵树右子树是否相同,树的子树是否相同是不是也需要用到上面说的条件,判断子树根结点的值,子树的左子树,子树的右子树,显然这是递归定义的,我们可以采用递归的方法进行问题的解决。

apprentice_eye的博客 344

题解:对称二叉树(101. Symmetric Tree)

判断二叉树是否对称是一个经典问题,常用的解法包括递归和迭代。递归方法自然直观,适合初学者理解,但可能会有栈溢出的问题。迭代方法通过队列来实现遍历,避免了栈溢出,适合处理深度大的树。

weixin_75156045的博客 542
上一篇: 27. Remove Element [easy] (Python)
下一篇: 107. Binary Tree Level Order Traversal II [easy] (Python)
coder_orz
博客等级 码龄14年 102粉丝 90原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值