Python3解leetcode 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
Note:
Bonus points if you could solve it both recursively and iteratively.
思路:二分类树的问题,可以考虑递归解法
# 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
def ismirror(root1,root2):
if root1 == None and root2 ==None:
return True
elif root1 == None or root2 ==None or root1.val != root2.val:
return False
return ismirror(root1.left,root2.right) and ismirror(root1.right,root2.left) return ismirror(root.left,root.right)
Python3解leetcode Symmetric Tree的更多相关文章
- Python3解leetcode N-ary Tree Level Order Traversal
问题描述: Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to ...
- Python3解leetcode Same Tree
问题描述: Given two binary trees, write a function to check if they are the same or not. Two binary tree ...
- Python3解leetcode Binary Tree Paths
问题描述: Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. E ...
- Python3解leetcode Binary Tree PathsAdd DigitsMove Zeroes
问题描述: Given an array nums, write a function to move all 0's to the end of it while maintaining the r ...
- Python3解leetcode Binary Tree PathsAdd Digits
问题描述: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...
- LeetCode: Symmetric Tree 解题报告
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- Python3解leetcode Same TreeBinary Tree Level Order Traversal II
问题描述: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...
- Python3解leetcode Average of Levels in Binary Tree
问题描述: Given a non-empty binary tree, return the average value of the nodes on each level in the form ...
- Python3解leetcode Subtree of Another Tree
问题描述: Given two non-empty binary trees s and t, check whether tree t has exactly the same structure ...
随机推荐
- The Google File System论文拜读
The Google File System Sanjay Ghemawat, Howard Gobioff, and Shun-Tak Leung Google∗ 摘要 我们设计并实现了谷歌文件系统 ...
- wpf自定义colorpicker
自定义colorpicker <ObjectDataProvider MethodName="GetType" ObjectType="{x:Type system ...
- Objective-C 内存管理之dealloc方法中变量释放处理
本文转载至 http://blog.sina.com.cn/s/blog_a843a8850101ds8j.html (一).关于nil http://cocoadevcentral.com/d/ ...
- 我的Java开发学习之旅------>使用Working Setst将Eclipse中的项目分类使项目一目了然
今天发现Eclipse中若有太多的项目,杂七杂八的,看起来会非常的痛苦.今天请教公司的前辈学会了一个方法,在Eclipse中,当项目比较多的时候,我们可以用WorkingSet将这些项目分类,把相关连 ...
- Error: Failed to fetch plugin E:_My_File______workMyCodemyCodecordova-workspaceplugman-testMyMath via registry. Probably this is either a connection problem, or plugin spec is incorrect.
$ cordova plugin add E:\_My_File_____\_work\MyCode\myCode\cordova-workspace\plugman-test\MyMath --sa ...
- Django 模型层--单表
ORM 简介 MTV或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这可以大大的减少了开 ...
- Django的模型层(2)---多表操作
多表操作 创建模型 实例:我们来假定下面这些概念,字段和关系 作者模型:一个作者有姓名和年龄. 作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息.作者详情模型和作者模型之间是一对 ...
- Zookeeper四字命令
ZooKeeper 支持某些特定的四字命令(The Four Letter Words)与其进行交互.它们大多是查询命令,用来获取 ZooKeeper 服务的当前状态及相关信息.用户在客户端可以通过 ...
- TopCoder<SRM>上的一道1100分的题目解析附代码
首先我们来简单看一下这道题的statement Problem Statement Note that in the following problem statement, all quo ...
- Python爬虫 —— 抓取美女图片
代码如下: #coding:utf-8 # import datetime import requests import os import sys from lxml import etree im ...