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. VNPY - windows 安装踩坑记录

    twisted requires PyHamcrest>=, which is not ins grin requires argparse>=1.1, which is not inst ...

  2. Python数据类型——字符串

    概论 字符串顾名思义就是一串字符,由于Python中没有“字符”这种数据类型,所以单个的字符也依然是字符串类型的.字符串可以包含一切数据,无论是能从键盘上找到的,还是你根本都不认识的.与数一样,字符串 ...

  3. 银行卡卡bin

    卡BIN指的是发卡行识别码,英文全称是 Bank Identification Number,缩写为 BIN.中文即“银行识别代码”  银行卡的卡号是标识发卡机构和持卡人信息的号码 一般是13-19位 ...

  4. 【转】使用iTextSharp在Asp.Net中操作PDF

    使用iTextSharp在Asp.Net中操作PDF操作 使用iTextSharp在Asp.Net中操作PDF系列文章目录 实战 iTextSharp iTextSharp 合并多个PDF文件 C#生 ...

  5. Go-ethereum源码解析-Part I

    1. 整体概览 makefile CANDY: .PHONY: geth android ios geth-cross swarm evm all test clean 已知phony 目标并非是由其 ...

  6. ProtocolBuffer for Objective-C Mac运行环境配置

    上班第4天,上司让我研究Google的Protocol Buffer,对于我这个小白来说这是一大难题.结合了一下网上资料,用了几个小时的时间,终于搞明白了.做个笔记,也当做资料给大家分享一下. 什么是 ...

  7. erlang中http请求

    1..app文件中 {application,backend,[  {description,"ebank backend"},  {vsn,1.0},  {modules,[]} ...

  8. ES6新语法的介绍

    对于ES6新语法,阮一峰有一篇文章介绍的挺详细 http://es6.ruanyifeng.com/#docs/destructuring

  9. Git 转载

    我每天使用 Git ,但是很多命令记不住. 一般来说,日常使用只要记住下图6个命令,就可以了.但是熟练使用,恐怕要记住60-100个命令. 下面是我整理的常用 Git 命令清单.几个专用名词的译名如下 ...

  10. 使用datepicker和uploadify的冲突解决(IE双击才能打开附件上传对话框)

    在开发的过程当中,IE的兼容无疑是我们的一块绊脚石,在我们使用的如期的datepicker插件和使用上传附件的uploadify插件的时候,两者就产生冲突,只要点击过时间的插件,uploadify上传 ...