[LeetCode]题解(python):101 Symmetric tree

时间:2022-10-01 20:05:52

题目来源


https://leetcode.com/problems/symmetric-tree/

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


题意分析


Input:一个二叉树

Output:True or False

Conditions: 判断一个二叉树是不是对称的


题目思路


一一对应判断值是否相等,注意要清楚谁对应谁。


AC代码(Python)

 # 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
"""
def help(p, q):
if p == None and q == None:
return True
if p and q and p.val == q.val:
return help(p.left, q.right) and help(p.right, q.left)
return False if root:
return help(root.left, root.right)
return True

[LeetCode]题解(python):101 Symmetric tree的更多相关文章

  1. <LeetCode OJ> 101. Symmetric Tree

    101. Symmetric Tree My Submissions Question Total Accepted: 90196 Total Submissions: 273390 Difficul ...

  2. [leetcode] 101. Symmetric Tree 对称树

    题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...

  3. Leetcode之101. Symmetric Tree Easy

    Leetcode 101. Symmetric Tree Easy Given a binary tree, check whether it is a mirror of itself (ie, s ...

  4. leetcode 100. Same Tree、101. Symmetric Tree

    100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...

  5. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

  6. 【LeetCode】101. Symmetric Tree (2 solutions)

    Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...

  7. LeetCode之“树”:Symmetric Tree && Same Tree

    Symmetric Tree 题目链接 题目要求: Given a binary tree, check whether it is a mirror of itself (ie, symmetric ...

  8. LeetCode(1) Symmetric Tree

    从简单的道题目開始刷题目: Symmetric Tree 题目:Given a binary tree, check whether it is a mirror of itself (ie, sym ...

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

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...

随机推荐

  1. 增加SWAP空间的方法

    增加swap空间的方法 背景:安装oracle数据库需要,需要设置swap空间为16G,当前swap空间只有4G,需要增加12Gswap空间. 1.创建一个空文件 # dd if=/dev/zero ...

  2. 【工具】CodeSmith Generator 7.0.2激活步骤

    学过三层的人应该认识CodeSmith Generator吧,今天我就跟大家一起探讨下CodeSmith Generator 7.0.2的激活,这最新版本破解的难度也是超越以往......具体看这篇日 ...

  3. a链接onclick="window.location.href=在ie6上面无法执行解决

    <a href="javascript:void(0)" onclick="window.location.href=document.getElementById ...

  4. Java 集合深入理解(3):Collection

    点击查看 Java 集合框架深入理解 系列, - ( ゜- ゜)つロ 乾杯~ 今天心情有点粉,来学学 Collection 吧! 什么是集合? 集合,或者叫容器,是一个包含多个元素的对象: 集合可以对 ...

  5. 一步一步学ZedBoard &amp&semi; Zynq&lpar;四&rpar;:基于AXI Lite 总线的从设备IP设计

    本帖最后由 xinxincaijq 于 2013-1-9 10:27 编辑 一步一步学ZedBoard & Zynq(四):基于AXI Lite 总线的从设备IP设计 转自博客:http:// ...

  6. 构件图 Component Diagram

    构件图是显示代码自身结构的实现级别的图表.构件图由诸如源代码文件.二进制代码文件.可执行文件或动态链接库 (DLL) 这样的构件构成,并通过依赖关系相连接 下面这张图介绍了构件图的基本内容: 下面这张 ...

  7. BZOJ 1062

    program candy bzoj1062; ; maxm=; maxn=; var n,len,m,i,p,t,l,r,c,d,q:longint; s:..,..maxn,..maxm] of ...

  8. STS中Maven配置

    最近接触maven, 配置过程中记录一下. STS是解压版的,启动后,可以看到已经有了Maven插件, , 但是,STS也同时给你了一个Maven,但是通常不建议使用STS自带的maven.使用默认的 ...

  9. multiWriter&period;go

    package blog4go import ( "errors" "fmt" ) var ( // ErrFilePathNotFound 文件路径找不到 E ...

  10. WPF DEV gridcontrol当前项的数据导出为mdb文件

    /// <summary> /// 导出为mdb /// </summary> /// <param name="sender"></pa ...