101. Symmetric Tree Leetcode Python

LeetCode 101 - A LeetCode Grinding Guide (C++ Version).pdf 立即下载

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

Note:

Bonus points if you could solve it both recursively and iteratively.

这道题目要求iterative 和recursive的解法。

recursive的解法比较简练

1.停止条件是 left==None & right==None

2. left.val==right.val 比较left.left right.right & left.right right.left

任何条件不成立都是false

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

class Solution:
    # @param root, a tree node
    # @return a boolean
    def sym(self,left,right):
        if left==None and right==None:
            return True
        if left and right and left.val==right.val:
            return self.sym(left.left,right.right) and self.sym(left.right,right.left)
        else:
            return False
            
    def isSymmetric(self, root):
        if root==None:
            return True
        return self.sym(root.left,root.right)        
            
        

如果是iterative的解法需要用两个stack去存node

1.先将left 和right 依次存入stack1 stack2

2.当stack非空的时候将node pop出来 依次往下取left 和right 

进行比较

代码如下

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

class Solution:
    # @param root, a tree node
    # @return a boolean
    def sym(self,left,right):
        if left==None and right==None:
            return True
        if left and right and left.val==right.val:
            return self.sym(left.left,right.right) and self.sym(left.right,right.left)
        else:
            return False
            
    def isSymmetric(self, root):
        if root==None:
            return True
        if root.left==None and root.right==None:
            return True
        if root.left==None or root.right==None:
            return False
        stack1=[]
        stack2=[]
        stack1.append(root.left)
        stack2.append(root.right)
        while len(stack1)!=0 and len(stack2)!=0:
            n1=stack1.pop()
            n2=stack2.pop()
            if n1.val!=n2.val:
                return False
            #if n1.left==None or n2.right==None:
             #   return False
            #if n1.right==None or n2.left==None:
             #   return False
            if n1.left==None and n2.right!=None or n1.left!=None and n2.right==None:
                return False
            if n1.right==None and n2.left!=None or n1.right!=None and n2.left==None:
                return False
            if n1.left and n2.right:
                stack1.append(n1.left)
                stack2.append(n2.right)
            if n1.right and n2.left:
                stack1.append(n1.right)
                stack2.append(n2.left)
        return True
        


Steger算法(Line_Gauss)-光条中心线提取(基于Hessian矩阵) Steger算法(Line_Gauss)-光条中心线提取(基于Hessian矩阵)算法背景介绍Hessian 矩阵与泰勒多项式关于求t导数与中心点、亚像素点高斯函数作用文献 算法背景介绍 在线结构光视觉传感器中,由线激光器发射出的线结构光,在本质上为一个连续且具有一定厚度的空间光平面,而在目标表面上所形成的具有一定宽度的光条特征,即为该光平面与目标表面相交而成的交线。在该空间光平面的厚度方向上,光强近似服从高斯分布,因而在摄像机采集到的光条图像中,在沿着光条宽度的方向或光条的法线方向上,其灰度也会呈现出类似 阅读详情

相关推荐

Ubuntu 22.04下GNU Radio 3.10全家桶编译避坑实录:从UHD、VOLK到WiFi收发模块gr-ieee802-11

本文详细介绍了在Ubuntu 22.04系统下编译安装GNU Radio 3.10全家桶的完整流程,包括UHD驱动、VOLK优化库以及WiFi信号处理模块gr-ieee802-11的集成。通过逐步指导解决版本兼容性和依赖问题,帮助研究人员快速搭建高效的软件定义无线电开发环境,特别适合使用USRP设备进行无线通信研究的开发者。

weixin_30825199的博客 446

LeetCode 101:和你一起你轻松刷题(C++)总篇章正在陆续更新

再从右往左遍历一遍,如果左边孩子的评分比右边的高,且左边孩子当前的糖果数不大于右边孩子的糖果数,则左边孩子的糖果数更新为右边孩子的糖果数加 1。在样例中,我们初始化糖果分配为 [1,1,1],第一次遍历更新后的结果为 [1,1,2],第二次遍历更新后的结果为 [2,1,2]在样例中,排序后的数组为 [[1,2], [1,3], [2,4]]。因此最终保留的区间为 [[1,2], [2,4]]。在这个样例中,我们可以移除区间 [1,3],使得剩余的区间 [[1,2], [2,4]] 互不重叠。

m0_60529319的博客 2066

AGV舵轮选型实战:从核心参数到精准计算的完整指南

本文提供了一份详尽的AGV舵轮选型实战指南,强调从经验决策转向数据驱动。文章系统讲解了如何将工况需求转化为核心参数,并通过牵引力、扭矩、功率等关键计算步骤,指导工程师完成精准选型,避免因选型不当导致的运行故障与效率低下。

醉三国 495

LeetCode-101. 对称二叉树(java)

金三银四,又到了刷题月。有时间的话,希望大家还是尽量刷起来咯。帮助往往不是直接体现出来的,但会让你对很多东西的底层实现了解的更深刻。

**My Coding Family** 1175

亲测解决This application failed to start because no Qt platform plugin could be initialized.

这个问题是由于环境变量改变或者Qt的路径改变导致,解决办法是在环境变量里面重新添加Qt路径。

狂小虎/Anthony Dave的博客 KuangXiaoHu's Blogs 2919

Python办公自动化 2.1开发环境搭建:PyCharm社区版配置Anaconda开发环境

本篇文章主要讲述了如何在windows主机上配置 PyCharm + Anaconda开发环境、以及了如何配置 PyQt界面开发环境,安装过程超级详细,初学者可以照着一步一步来!

超级大洋葱的博客 2584

LeetCode 101. 对称二叉树

🌈🌈😄😄 欢迎来到茶色岛独家岛屿,本期将为大家揭晓LeetCode 101. 对称二叉树,做好准备了么,那么开始吧。 🌲🌲🐴🐴

weixin_62275996的博客 760

AIGC 算法工程师面试八股文全解析:Diffusion Models 原理、推导与实战要点

特性 | 重参数化技巧 (Reparameterization Trick) |本质| 将随机变量的采样过程从不可导的随机操作转换为可导的确定性函数与一个已知分布的随机噪声的组合。目的。

m0_71589036的博客 402

【经典算法】LeetCode101:对称二叉树(Java/C/Python3实现含注释说明,Easy)

方法优点缺点时间复杂度空间复杂度递归法- 直观易懂- 代码相对简洁- 可能导致函数调用栈溢出的风险- 需要额外的空间来存储函数调用栈O(n)O(n)队列法- 不会导致函数调用栈溢出的风险- 无需递归,代码较为直观- 灵活的节点入队和出队顺序- 需要手动维护队列数据结构和追踪节点的层次- 需要额外的空间来存储队列和节点的信息O(n)O(m)

2072

路由三大分类:直连路由、静态路由、动态路由

直连路由、静态路由和动态路由各有其独特的特点和适用场景。直连路由适用于简单的直接连接网络,静态路由提供了精确的路径控制,而动态路由则在大型复杂网络中展现出强大的适应性和自动化能力。了解和掌握这三种路由类型的配置和管理,对于网络管理员来说,是确保网络高效、安全运行的重要技能。

网络技术联盟站 4697

科研问答:医药类本科生(临床、中医、中药与护理)本科阶段是否需要开展科研学习?

医药类本科生(临床、中医、中药与护理)本科阶段是否需要开展科研学习?

MD Code 1506

LeetCode 101:和你一起你轻松刷题,手撕算法

电子书资源 免解压: 链接: https://pan.baidu.com/s/1f5eJNL8Qamj6xB1bMti_fw 提取码: sq5v (无偿分享,点个赞再走哦) 如果你刷 Leetcode 觉得吃力,那么一定需要这份谷歌大佬的 Leetcode 刷题笔记!每一道题的题解都写得非常清楚。 ...

封神之路 2631

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

Leetcode【101】Symmetric Tree (Python版)

python函数中定义函数,只能这么写 # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class S...

ssjdoudou的博客 7292

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

LeetCode 101.Symmetric Tree python

题目描述:    给定一个二叉树,检查它是否镜像对称的。例如二叉树[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分析:    递归调用check函数,如果两个节点都是空,则返回tr...

ma412410029的博客 342

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

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

对称树DescriptionExample题意解题思路code 101. Symmetric Tree Easy Description Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). Example For example, this binary tre...

。 347

Leetcode 101. Symmetric Tree (python+cpp)

Leetcode 101. Symmetric Tree题目解法: 题目 解法: 判断一个树是否对称等价于判断左右子树是否对称,一般用四步法解决: 如果两个子树都为空指针,则它们相等或对称 如果两个子树只有一个为空指针,则它们不相等或不对称 如果两个子树根节点的值不相等, 则它们不相等或不对称 根据相等或对称要求,进行递归处理 python代码如下: class Solution: def isSymmetric(self, root: TreeNode) -> bool:

qq_37821701的博客 275

LeetCode之 101. Symmetric Tree python

给定一个二叉树,检查它是否是镜像对称的。例如,二叉树 [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 说明:如果你可以运用递归和迭代两种方法解决这个问题,会很加分。递归方...

新雪兰 463

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的博客 392

【leetcode-Python】-Tree-101. Symmetric Tree

题目链接 https://leetcode.com/problems/symmetric-tree/ 题目描述 给定二叉树,判断这棵二叉树是否是对称二叉树,即树的结构是否镜像对称的。 示例 二叉树[1,2,2,3,4,4,3]是镜像对称的,返回True: 1 / \ 2 2 / \ / \ 3 4 4 3 二叉树[1,2,2,null,3,null,3]不是镜像对称的,返回False: 1 / \ 2 2 \ \ 3

gulaixiangjuejue的博客 196

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的博客 277

[leetCode By Python]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 / \ / \ 3

sharonITlion的博客 332

【LeetCode】101. Symmetric Tree 解题报告(Python)

题目分析: 给定一个二叉树,检查它是否是镜像对称的。 例如,二叉树 [1,2,2,3,4,4,3] 是对称的。 但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的: 解题思路: 这一题的本质还是遍历,对于树的遍历可使用递归或者栈。 提交代码1:(递归,Runtime: 40 ms, faster than 99.14% ) class Solution: def...

L141210113的专栏 624

[leetcode] 101. 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 But the follo...

闲庭信步 286

[leetcode:python]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 / \ / \ 3 4 4 3But

cyy2learn的博客 573

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

Lu_gee的博客 396

leetcode 101. Symmetric Tree 递归和迭代 python3

一.问题描述 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...

CSerwangjun的博客 360
上一篇: 27. Remove Element Leetcode Python
下一篇: 12. Integer to Roman Leetcode Python
hyperbolechi
博客等级 码龄12年 7粉丝 178原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值