题目来源:
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的更多相关文章

  1. 【Leetcode_easy】965. Univalued Binary Tree

    problem 965. Univalued Binary Tree 参考 1. Leetcode_easy_965. Univalued Binary Tree; 完

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 【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 ...

  6. 【LeetCode】965. Univalued Binary Tree 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...

  7. [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 ...

  8. LeetCode.965-单一二叉树(Univalued Binary Tree)

    这是悦乐书的第366次更新,第394篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第228题(顺位题号是965).如果树中的每个节点具有相同的值,则二叉树是单一的.当且仅 ...

  9. LeetCode题解之Univalued Binary Tree

    1.题目描述 2.问题分析 遍历一遍树,然后将所有节点的数值放入到一个set中,最后检查set中元素的个数是否为1. 3.代码 bool isUnivalTree(TreeNode* root) { ...

随机推荐

  1. Java Web应用开发工具

    java Web应用开发工具详细地址:https://my.oschina.net/gitosc/blog/1538466

  2. (文章也有问题,请自行跳过)react中的状态机每次setState都是重新创建新的对象,如需取值,应该在render中处理。

    demo如下 class Demo4StateLearn extends React.Component { constructor(props) { super(props); this.state ...

  3. linux 用户管理修改用户信息、密码状态、删除用户、退出登陆、切换用户

    修改用户信息usermoduseradd支持的选项usermod都支持passwd有两个选项-l(在密码串前面加了两个叹号),-u,usermod有两个选项:-L 临时锁定用户(Lock)(在密码串前 ...

  4. sql 字段别名里包含特殊字符

    select ename employee.name from emp; 在数据库查询时,如果列名的别名里特殊符号,报错. select ename 'employee.name' from emp; ...

  5. 浅谈 java 反射机制

    一:Java反射概念 Java反射是Java被视为动态(或准动态)语言的一个关键性质.这个机制允许程序在运行时透过Reflection APIs取得任何一个已知名称的class的内部信息,包括其mod ...

  6. Latex 数学公式使用入门

    Latex 数学公式使用示例 Latex 数学公式命令中,数学符号都使用反斜杠(backslash, '\')转义英文缩略词 , 一些简单的数学符号命令: 其使用大括号(curly braces, ' ...

  7. AIX用裸设备给表空间添加数据文件

    近期在对生产数据库表空间进行扩容,目的是春节期间保证表空间的使用率,不会出现紧急告警信息. 1.查看表空间使用率的SQL语句 col tablespace_name for a16 col SUM_S ...

  8. [源码] YoCelsius

    YoCelsius 视频查看地址 苹果商店免费下载 最美应用介绍 源码地址     未完成的功能 [说明] 1. 本人写了几乎所有的显示控件与动画效果 2. 希望有人能喜欢,下载软件后给个好评 3. ...

  9. Redis学习---基础学习[all]

    什么是NoSQL型数据库 NoSQL数据库---NoSQL数据库的分类 Redis学习---NoSQL和SQL的区别及使用场景 Redis学习---负载均衡的原理.分类.实现架构,以及使用场景 什么是 ...

  10. Nginx 泛解析配置请求映射到多端口实现二级域名访问

    由于想实现一个域名放置多个应用运行的目的,而不想通过域名后加端口号方式处理,这种方式处理记起来太麻烦,偷懒党简直不能忍,故而考虑了使用二级域名来处理多个应用同时运行.Google了一番资料并进行了尝试 ...