[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 = ...
随机推荐
- [代码审计]云ec电商系统代码审计
0x00 前言 看了一下博客内最新的文章,竟然是3月28号的,一个多月没写文章了,博客都长草了. 主要是临近毕业,事情繁多,也没有啥时间和心情静下来写.. 不过现在的话,毕业的东西告一段落了,几乎没啥 ...
- BZOJ3009 : 集合
取一棵生成森林,根据题目限制可得,与一个点相连的多余的边数是$O(\sqrt{m})$级别的. 对于树边,每个点维护3棵权值线段树,依次保存它的儿子里各个集合的边. 再开3*3个分块数组,记录多余边以 ...
- 解决Windows x86网易云音乐不能将音乐下载到SD卡的BUG
由于我个人最常用的电脑是Surface pro4 256G版本,装了不少生产力空间还挺吃紧的,音乐之类的必然都存单独的SD卡里.用UWP版本的网易云音乐倒是没问题,最近问题来了,UWP版本的网易云音乐 ...
- GitLab目录迁移方法
在生产环境上迁移GitLab的目录需要注意一下几点: 1.目录的权限必须为755或者775 2.目录的用户和用户组必须为git:git 3.如果在深一级的目录下,那么git用户必须添加到上一级目录的账 ...
- SGU 101 Domino (输出欧拉路径)
101. Domino time limit per test: 0.25 sec. memory limit per test: 4096 KB Dominoes – game played wit ...
- 从零开始部署CAS服务器
从0开始部署CAS服务器的操作过程文档,我已经整理完毕,一共分为8步,这8步都是我自己操作实践过的. Setp1:Ubuntu server安装 在virtual box中安装ubuntu serve ...
- HDU 4568 SPFA + TSP
这道题是长沙邀请赛的题,当时是道签到题. 这种题还是很常见的,讲一下思路. 首先是预处理出每个宝藏之间的距离,还有到边的距离,直接对每个宝藏进行一次SPFA就可以了. 然后就是经典的求TSP的过程. ...
- google打不开怎么办?谷歌打不开的解决方法
www.ggfwzs.com 我是在这里安装插件,安装后可以打开google http://jingyan.baidu.com/article/b907e627d67ad646e6891c52.htm ...
- Revit API修改保温层厚度
start [Transaction(TransactionMode.Manual)] [Regeneration(RegenerationOption.Manual)] ;, newLayer); ...
- On premise TFS Git OAuth clone failed on agent
利用TFS2015的跨平台生成代理,在mac osx 上生成xamarin.ios项目,agent费劲九牛二虎之力搞定了(参见http://www.cnblogs.com/zjoch/p/581101 ...