965. Univalued Binary Tree
题目来源:
https://leetcode.com/problems/univalued-binary-tree/submissions/
自我感觉难度/真实难度:
题意:
分析:
自己的代码:
class Solution(object):
def isUnivalTree(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
return self.dsf(root.val,root)
def dsf(self,val,roo):
if not roo:return True
if roo.val!=val:
return False
return self.dsf(val,roo.left) and self.dsf(val,roo.right)
代码效率/结果:
Runtime: 36 ms, faster than 52.18% of Python online submissions for Univalued Binary Tree.
优秀代码:
class Solution(object):
def isUnivalTree(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
q=collections.deque()
q.append(root)
val=root.val
while q:
node=q.popleft()
if not node: continue
if val!=node.val:return False
q.append(node.left)
q.append(node.right)
return True
代码效率/结果:
Runtime: 32 ms, faster than 97.16% of Python online submissions for Univalued Binary Tree.
自己优化后的代码:
反思改进策略:
1.对怎么使用迭代,还是不熟悉,只是会使用固定的模板
2.递归需要考虑的两个核心:递归结束的出口,哪个是递归变量
965. Univalued Binary Tree的更多相关文章
- 【Leetcode_easy】965. Univalued Binary Tree
problem 965. Univalued Binary Tree 参考 1. Leetcode_easy_965. Univalued Binary Tree; 完
- LeetCode 965. Univalued Binary Tree
A binary tree is univalued if every node in the tree has the same value. Return true if and only if ...
- LeetCode 965 Univalued Binary Tree 解题报告
题目要求 A binary tree is univalued if every node in the tree has the same value. Return true if and onl ...
- LC 965. Univalued Binary Tree
A binary tree is univalued if every node in the tree has the same value. Return true if and only if ...
- 【leetcode】965. Univalued Binary Tree
题目如下: A binary tree is univalued if every node in the tree has the same value. Return true if and on ...
- 【LeetCode】965. Univalued Binary Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...
- [Swift]LeetCode965. 单值二叉树 | Univalued Binary Tree
A binary tree is univalued if every node in the tree has the same value. Return true if and only if ...
- LeetCode.965-单一二叉树(Univalued Binary Tree)
这是悦乐书的第366次更新,第394篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第228题(顺位题号是965).如果树中的每个节点具有相同的值,则二叉树是单一的.当且仅 ...
- LeetCode题解之Univalued Binary Tree
1.题目描述 2.问题分析 遍历一遍树,然后将所有节点的数值放入到一个set中,最后检查set中元素的个数是否为1. 3.代码 bool isUnivalTree(TreeNode* root) { ...
随机推荐
- 小tip:巧用CSS3属性作为CSS hack——张鑫旭
一.开篇小问题 题目:实现类似下图的宽度自适应效果,IE9+,FireFox,Chrome,Opera等使用CSS3实现,IE6~8浏览器使用图片实现. 计时思考…… 二.思考中 ————- 假设这是 ...
- drupal7 addExpression+union+分页
global $user; $query_single = db_select('mp_order_singlepay', 'ms') ->fields('ms', array('order_i ...
- R包下载的一些小问题
install.packages(c("matrixStats", "Hmisc", "splines", "foreach&qu ...
- Spring Boot—18Redis
pom.xml <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-ja ...
- CSS的设计模式
什么是设计模式? 曾有人调侃,设计模式是工程师用于跟别人显摆的,显得高大上:也曾有人这么说,不是设计模式没用,是你还没有到能懂它,会用它的时候. 先来看一下比较官方的解释:“设计模式(Design p ...
- sqlserver 统计信息自动更新 ----trace flag 2371
对于已经应用的表(非临时表)统计信息更新条件为500+20%,具体更新条件 If the statistics is defined on a regular table, it is out of ...
- 几个很好用SQL语法(SqlServer)
1,MERGE INTO 语句: 这个语法仅需要一次全表扫描就完成了全部工作,执行效率要高于INSERT+UPDATE,作用还是很强大的(简单的说就是它可以批量更新和插入处理一个数据集,如果存在就更新 ...
- include方便查找
#include <assert.h> //设定插入点#include <ctype.h> //字符处理#include <errno.h> //定义错误码#inc ...
- [翻译] CSStickyHeaderFlowLayout
CSStickyHeaderFlowLayout https://github.com/jamztang/CSStickyHeaderFlowLayout Parallax, Sticky Heade ...
- LocationCoder 地图经纬度解析
LocationCoder 地图经纬度解析 其实,在地图里面将地图解析成有意义的地址,或者把地址转换成有意义的经纬度都是很容易的事情,只是我将其封装了支持KVO,通知中心,block取结果,代理取结果 ...