[LeetCode&Python] Problem 101. Symmetric Tree
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的更多相关文章
- 【leetcode❤python】101. Symmetric Tree
#-*- coding: UTF-8 -*-# Definition for a binary tree node.# class TreeNode(object):# def __init_ ...
- <LeetCode OJ> 101. Symmetric Tree
101. Symmetric Tree My Submissions Question Total Accepted: 90196 Total Submissions: 273390 Difficul ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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, ...
- [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 ...
- [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 ...
随机推荐
- Cognos集成至portal平台运行报表时只出“#”
1. 问题描述 报表集成到平台后,运行报表过程中,当多次运行后,页面只显示“#” 2. 问题分析 这是因为浏览器筛选器限制问题 3. 解决方案 在IE浏览器设置中,Internet选项-安全-自定义级 ...
- es6,es7,es8
概述 ES全称ECMAScript,ECMAScript是ECMA制定的标准化脚本语言.目前JavaScript使用的ECMAScript版本为ECMAScript-262. ECMAScript 标 ...
- 什么是ORM?
什么是ORM? MVC框架中重要的一部分就是ORM,实现了数据模型与数据库的解耦,即数据模型不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库. ORM是对象关系映射的简称,主要任务是: 根 ...
- VS2017提醒找不到MSVCR110D.dll
我的电脑时win10我已解决,不能传文件,需要联系我
- 《程序设计入门——C语言》翁恺老师 第四周编程练习记录
1 奇偶个数(5分) 题目内容: 你的程序要读入一系列正整数数据,输入-1表示输入结束,-1本身不是输入的数据.程序输出读到的数据中的奇数和偶数的个数. 输入格式: 一系列正整数,整数的范围是(0,1 ...
- js查漏补缺
js中: 1.对空(Null).未定义(Undefined).Symbol .函数(Function) 都是数据类型(js不像java中,声明了变量会有默认值,在js中只声明变量而没有赋值的时候,类型 ...
- centos6.5删除/boot后恢复
删除/boot下的所有东西 更改为从光盘启动并进入紧急救援模式 语言选择英语 选择美式键盘布局 不配置网络 选择Continue 这里提示系统已经被挂载到了/mnt/sysimage 选择shell, ...
- java切割音频文件
工具: 一个jar包即可:jave-1.0.2.jar 可以切割wav格式的音频文件 完整工程目录 就一个jar包,一个main类 代码: package com.zit; import java.i ...
- Shell脚本 自动部署 SpringBoot 应用
公司项目使用了SpringBoot.开发的应用需要自动上传到服务器.虽然目前对热部署还没完全掌握.先使用shell简化一下部署吧. # 上传密钥 sshLoginKey=/f/MyFile/root. ...
- mysql Navicat客户端
Navicat是一个用来操作多种数据库的客户端. 可应用操作系统:Windows.macOS.Linux. 可应用 Navicat 产品:Navicat for MySQL.Navicat for P ...