[leetcode]Symmetric Tree @ Python
原题地址:https://oj.leetcode.com/problems/symmetric-tree/
题意:判断二叉树是否为对称的。
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following is not:
1
/ \
2 2
\ \
3 3
解题思路:这题也不难。需要用一个help函数,当然也是递归的。当存在左右子树时,判断左右子树的根节点值是否相等,如果想等继续递归判断左子树根的右子树根节点和右子树根的左子树根节点以及左子树根的左子树根节点和右子树根的右子树根节点的值是否相等。然后一直递归判断下去就可以了。
代码:
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param root, a tree node
# @return a boolean
def help(self, p, q):
if p == None and q == None: return True
if p and q and p.val == q.val:
return self.help(p.right, q.left) and self.help(p.left, q.right)
return False def isSymmetric(self, root):
if root:
return self.help(root.left, root.right)
return True
[leetcode]Symmetric Tree @ Python的更多相关文章
- LeetCode: Symmetric Tree 解题报告
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- leetcode:Symmetric Tree【Python版】
#error caused by:#1:{} 没有考虑None输入#2:{1,2,2} 没有控制h和t#3:{4,-57,-57,#,67,67,#,#,-97,-97} 没有考虑负号,将s从str变 ...
- [LeetCode] Symmetric Tree 判断对称树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- LeetCode——Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 二叉树系列 - [LeetCode] Symmetric Tree 判断二叉树是否对称,递归和非递归实现
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- [Leetcode] Symmetric tree 对称二叉树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- Python3解leetcode Symmetric Tree
问题描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...
- LeetCode() Symmetric Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- leetcode Same Tree python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
随机推荐
- python3.5 自带的虚拟环境使用
首先我们要选择一个目录作为虚拟环境的目录, 这里选择c:\myenv cd myenv python -m venv . #在当前目录下创建虚拟环境 创建完成之后,myenv下会多出一些文件 进入sc ...
- python语法(五)—函数
前面几天学习了python的基础语法,判断,循环,以及文件操作等等内容,对python也是有了一个认识.今天开始学习python的函数和模块. 函数 函数是什么?我的理解就是,他和java中的方法是一 ...
- android studio svn 创建分支
创建分支或标签 从哪里复制 工作副本 用这个变体去创建分支,并带着当地的改变.通常,服务项将被 添加带历史 , 不仅仅只有目标目录. 每个不同于根的版本文件 ,将被指定的复制.它推荐 去更新 工作副本 ...
- Sed&awk笔记之awk篇(转)
Awk是什么 Awk.sed与grep,俗称Linux下的三剑客,它们之间有很多相似点,但是同样也各有各的特色,相似的地方是它们都可以匹配文本,其中sed和awk还可以用于文本编辑,而grep则不具备 ...
- Timer triggered DMA transfer - Delay between requesting the DMA transfer
Hello, I'm working with a STM32F407 controller board. Right now, I want to trigger a DMA transfer ...
- CRC32 of Ether FCS with STM32
Everyone knows that STM32F1xx, STM32F2xx, STM32F4xx have a hardware unit with a polynomial CRC32 0x0 ...
- ElasticSearch入门 :Windows下安装ElasticSearch
这是ElasticSearch 2.4 版本系列的第一篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...
- delphi 取上级目录
var S: string;begin S := 'c:\aa\bb\cc\dd\abc.exe'; ShowMessage(ExtractFileDir(ExtractFileDir(S))) ...
- delphi 线程的应用 和spcomm的应用
http://bbs.csdn.net/topics/390744417 串口控件本身的线程不是这样理解的,你不用管它本身用不用线程,它的内部线程和你也没关系.前面说过了,你可以在自己的主线程里创建好 ...
- 排名前16的Java工具类
原文:https://www.jianshu.com/p/9e937d178203 在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法 ...