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) { ...
随机推荐
- Linux : task work 机制
task work机制可以在内核中向指定的进程添加一些任务函数,这些任务函数会在进程返回用户态时执行,使用的是该进程的上下文.包括下面的这些API: task_work_add task_work_c ...
- HTML5新增的form属性简介——张鑫旭
一.引言 HTML5中新增了一个名为form的属性,是一个与处理表单相关的元素. 在HTML4或XHTML中,我们要提交一个表单,必须把相关的控件元素都放在<form>元素下.因为表单提交 ...
- 关于Google圆角高光高宽自适应按钮及其拓展
关于Google圆角高光高宽自适应按钮及其拓展————源自张鑫旭css讲解 这篇文章发布于 2009年10月24日,星期六,18:08,归类于 css相关. 阅读 48770 次, 今日 1 次 by ...
- CentOS 7 搭建PXC 数据库集群
CentOS 7 搭建PXC 数据库集群 PXC( Percona XtraDB Cluster ) 特点如下: 1.同步复制,事务要么在所有节点提交或不提交,保证了数据的强一致性. 2.多主复制,可 ...
- ArcGIS Google Map 增加虚拟图层(MapImageLayer)(转载)
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...
- Java实验案例(接口)
实验任务 任务一:设计实现发声接口 任务二:动物乐园 实验内容 任务一:设计实现发声接口 任务目的: 理解并掌握如何定义接口 掌握接口的实现方式 任务描述: 设计和实现一个Soundable接口,该接 ...
- chrome 浏览器调用 ocx 插件
原文:http://blog.csdn.net/wangchao1988ok/article/details/45193489 IE 上使用 ocx 插件网上已经有很多资料说明,这里就不重复了,那如何 ...
- SCLAlertView-Swift
SCLAlertView-Swift https://github.com/vikmeup/SCLAlertView-Swift Animated Alert View written in Sw ...
- [swift] Async
Async https://github.com/duemunk/Async Syntactic sugar in Swift for asynchronous dispatches in Grand ...
- UIImagePickerController按钮的中文问题
UIImagePickerController按钮的中文问题 执行以下两步即可 1. 在targets中设置region为China 2. 在project中添加支持中文