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. 深入理解hashCode

    1.hashCode的概念 (1)hashCode方法是Object类的方法,在Java里所有类都默认继承Object类,即所有类都有hashCode方法. (2)hashCode是jdk根据对象的存 ...

  2. echarts 去掉 x轴坐标

    symbol:'none', //这句就是去掉点的 smooth:true,

  3. 帝国cms中当调用当前信息不足时,继续取其他数据

    <?php$sql=$empire->query("select * from table1 order by id limit 20"); $num = mysql_ ...

  4. ORM概念

    ORM概念: 指对象结构和数据库架构间的映射,对象和数据库架构有一定的映射关系,让程序员可以不必编写sql

  5. abap将内表数据导出为excel文件

    一个不错的方案: WHEN 'EXPORT'. "导出数据 DATA : GT_TEMP TYPE TABLE OF TY_ITEM WITH HEADER LINE. LOOP AT GT ...

  6. ie11~ie9兼容的布局写法。bootsteap的12栅格,栅格化就可以实现。

    全局 CSS 样式 设置全局 CSS 样式:基本的 HTML 元素均可以通过 class 设置样式并得到增强效果:还有先进的栅格系统. 概览 深入了解 Bootstrap 底层结构的关键部分,包括我们 ...

  7. Saiku数据库迁移H2迁移到Mysql(二十二)

    Saiku数据库迁移H2迁移到Mysql Saiku默认使用H2数据库来存储saiku的用户与角色信息,我们可以根据角色来做saiku的权限控制,然后将角色分配给用户 ,该用户就会有对应的约束了! 由 ...

  8. ubuntu 16.04 安装 opencv +contrib (3.2.0) + python 3.5

    环境: - ubuntu 16.04 - OpenCV + contrib 3.2.0 (文中附下载链接) - Python 3.5 基于其他环境的配置应该大同小异. 没时间解释了,直接上车. 更新下 ...

  9. Python基础-Python的三元运算符和lambda表达式

    1. Python的三元表达式: 现在大部分高级语言都支持 “?”这个三元运算符,它对应的表达式如下:condition ? value if true:value if else 但是 Python ...

  10. 介绍一下Spring Cloud Config

    Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持.使用Config Server,您可以在所有环境中管理应用程序的外部属性.客户端和服务器上的概念映射与Spring ...