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 following [1,2,2,null,3,null,3] is not:

    1
/ \
2 2
\ \
3 3

BFS and Iterative:

# 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
"""
que=[root]
while que:
check=[]
n=len(que)
for i in range(n):
node=que.pop(0)
if node:
que.append(node.left)
que.append(node.right)
check.append(node.val)
else:
check.append(None)
n=len(check)
for i in range(n):
if check[i]!=check[n-i-1]:
return False
return True

  

Recursion:

# 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 findsys(node1,node2):
if node1==None and node2==None:
return True
if node1==None or node2==None:
return False
return node1.val==node2.val and findsys(node1.left,node2.right) and findsys(node1.right,node2.left) return findsys(root,root)

  

[LeetCode&Python] Problem 101. Symmetric Tree的更多相关文章

  1. 【leetcode❤python】101. Symmetric Tree

    #-*- coding: UTF-8 -*-# Definition for a binary tree node.# class TreeNode(object):#     def __init_ ...

  2. <LeetCode OJ> 101. Symmetric Tree

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

  3. [LeetCode&Python] Problem 257. Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

  4. [LeetCode&Python] Problem 107. Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  5. [LeetCode&Python] Problem 563. Binary Tree Tilt

    Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the ab ...

  6. [LeetCode&Python] Problem 100. Same Tree

    Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...

  7. [LeetCode&Python] Problem 429. N-ary Tree Level Order Traversal

    Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  8. [LeetCode&Python] Problem 589. N-ary Tree Preorder Traversal

    Given an n-ary tree, return the preorder traversal of its nodes' values. For example, given a 3-ary  ...

  9. [LeetCode&Python] Problem 590. N-ary Tree Postorder Traversal

    Given an n-ary tree, return the postorder traversal of its nodes' values. For example, given a 3-ary ...

随机推荐

  1. springboot测试的方法

    @RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest( classes = {App.class})@WebAppConfigurationpu ...

  2. vue-calendar 基于 vue 2.0 开发的轻量,高性能日历组件

    vue-calendar-component 基于 vue 2.0 开发的轻量,高性能日历组件 占用内存小,性能好,样式好看,可扩展性强 原生 js 开发,没引入第三方库 Why Github 上很多 ...

  3. maven配置及使用

    配置maven工程.从官网下载maven工具,然后解压到磁盘某个目录下即可. 计算机->属性->高级系统设置->环境变量. 新建如下变量: 变量名:MAVEN_HOME 变量值:C: ...

  4. db2常用语句

    连接数据库 db2 connect to chmgmdb user ch_mgm 断开数据库 db2 disconnect current 查询 db2 "select * from btp ...

  5. flask 第二章 endpoint重名 Flask路由 初始化配置 Falsk Config 蓝图+目录结构

    今日内容 1.路由的分发,以下两种方式效果一样,但是都能指向同一个函数 from flask import Flask app=Flask(__name__) #第一种方式 @app.route('/ ...

  6. 通读SCRUM实战指南教材,提出5个问题。

    问题一:为什么要制定优先级的排定和调整? 创建一个排好优先级的项目组合并讲重点放在转移团队上,如果做得对,可以消除在项目数量超过团队能力 的情况下过于常见的多任务处理. 问题二:为什么我们要做文档? ...

  7. rabbitmq 日志存储路径

    Linux      下/var /log/rabbitmq/ windows下C:\Users\Administrator\AppData\Roaming\RabbitMQ\log

  8. vue+uwsgi+nginx部署路飞学城

    vue+uwsgi+nginx部署路飞学城   有一天,老男孩的苑日天给我发来了两个神秘代码,听说是和mjj的结晶 超哥将这两个代码,放到了一个网站上,大家可以自行下载 路飞学城django代码 ht ...

  9. sql 约束Check中使用Case函数

    CHECK 约束用于限制列中的值的范围 在Check中使用Case函数在很多情况下都是非常不错的解决方法.可能有很多人根本就不用Check,那么我建议你在看过下面的例子之后也尝试一下在SQL中使用Ch ...

  10. asp调用短信接口实现用户注册

    前几天做一个asp语言开发的网站需要实现用户注册短信验证功能,就研究了一下如何实现,简单给大家分享下调用过程. 首先需要找到一个第三方短信接口,当时用的是动力思维乐信的短信接口. 首先需要先注册个动力 ...