对称二叉树

给定一个二叉树,检查它是否是镜像对称的。

例如,二叉树 [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 思路1:使用层次遍历解决,如果每一层都是对称的 那么该二叉树为对称(正好先做的层次遍历,发现这里可以直接用同样思路做,把空节点用' '空格代替 以保证对称)
 # Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
def isSymmetric(self, root: TreeNode) -> bool:
if root == None:
return True
layer = [root]
res = []
while len(layer):
this_res = []
next_l = []
for n in layer:
if n == ' ':
this_res.append(' ')
continue
this_res.append(n.val)
if n.left:
next_l.append(n.left)
else:
next_l.append(' ')
if n.right:
next_l.append(n.right)
else:
next_l.append(' ')
for i in range(len(this_res)//2):
if this_res[i] != this_res[len(this_res)-i-1]:
return False
layer = next_l return True

递归判断:

 # Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
def judge(self, left, right):
if left == None and right == None:
return True
if left != None and right == None:
return False
if left == None and right != None:
return False
if left.val != right.val:
return False
return self.judge(left.right, right.left) and self.judge(left.left, right.right) def isSymmetric(self, root: TreeNode) -> bool:
if root == None:
return True
return self.judge(root.left, root.right)

迭代:

def isSymmetric(self, root):
if not root:
return True
nodeList = [root.left,root.right]
while nodeList:
symmetricLeft = nodeList.pop(0)
symmetricRight = nodeList.pop(0)
if not symmetricLeft and not symmetricRight:
continue
if not symmetricLeft or not symmetricRight:
return False
if symmetricLeft.val != symmetricRight.val:
return False
nodeList.append(symmetricLeft.left)
nodeList.append(symmetricRight.right)
nodeList.append(symmetricLeft.right)
nodeList.append(symmetricRight.left)
return True
												

LeetCode 101 对称二叉树的几种思路(Python实现)的更多相关文章

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

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

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

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

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

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

  4. php实现求对称二叉树(先写思路,谋而后动)

    php实现求对称二叉树(先写思路,谋而后动) 一.总结 1.先写思路,谋而后动 二.php实现求对称二叉树 题目描述: 请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的 ...

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

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

  6. Leetcode题目101.对称二叉树(简单)

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

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

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

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

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

  9. 【Leetcode】对称二叉树

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

随机推荐

  1. Python爬虫利器一之Requests库的用法

    前言 之前我们用了 urllib 库,这个作为入门的工具还是不错的,对了解一些爬虫的基本理念,掌握爬虫爬取的流程有所帮助.入门之后,我们就需要学习一些更加高级的内容和工具来方便我们的爬取.那么这一节来 ...

  2. MyBatis 实用篇(一)入门

    MyBatis 实用篇(一)入门 MyBatis(http://www.mybatis.org/mybatis-3/zh/index.html) 是一款优秀的持久层框架,它支持定制化 SQL.存储过程 ...

  3. c#解决Nullable类型的转换 (包含DataContract的序列化和反序列化以及 该例子应用在反射属性setvalue的时候有用)

    using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Reflect ...

  4. idea注释字体倾斜的解决办法

    File-->Settings-->Editor--> Color Scheme-->Language Defaults-->Comments-->Line con ...

  5. Converting HTML to PDF with pdfHTML

    https://itextpdf.com/itext7/pdfHTML pdfHTML 的一个例子 一个基本的例子将显示使用 pdfHTML.为此, 我们将使用下面的 HTML 和 CSS. < ...

  6. Redis数据结构(六)

    Redis数据结构(Sort-set)(游戏排名和微博热点话题排名上应用): 特点:可存储有序但不重复的数据,根据分数指定存储顺序 1 Sort-set和Set的区别: (1)sort的每个成员都是以 ...

  7. UVa 11136 Hoax or what (STL)

    题意:有 n 天,每天有m个数,开始的前一天没有数据,然后每天从这个里面拿出一个最大的和最小的,求 n 天的最大的和最小的差值相加. 析:一看就知道用set啊,多简单的STL,不过要注意,开long ...

  8. HDU1232 畅通工程 2017-04-12 19:20 53人阅读 评论(0) 收藏

    畅通工程 Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submissi ...

  9. linux gitlab-ctl reconfigure报错问题修复 502

    Running handlers: There was an error running gitlab-ctl reconfigure: bash[migrate gitlab-rails datab ...

  10. Solr中的一些查询参数

    fl: 是逗号分隔的列表,用来指定文档结果中应返回的 Field 集.默认为 “*”,指所有的字段. defType: 指定query parser,常用defType=lucene, defType ...