题目来源:
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. php excel原理

  2. (一)windows下安装MongoDB解压版

    windows下安装MongoDB解压版 我安装的是(mongodb-win32-x86_64-2008plus-ssl-4.0.8.zip)版本 亲测可以使用. 连接: Windows下MongoD ...

  3. drupal7 模糊查询接口

    $query->condition('card_no', db_like($batch_no).'%', 'LIKE');

  4. 菜鸟的HTML学习之路

    开发网站流程 确定风格.功能(论坛.留言板.支付.用户登录等). 美工制作网页效果图(首页.列表页.内容页). 制作人员切图排版,排成网页形式. 后台程序开始写程序. 前台与后台结合. HTML注释 ...

  5. Django Redis配合Mysql验证用户登录

    1.redis_check.py # coding:utf-8 import pymysql import redis import sys def con_mysql(sql): db = pymy ...

  6. 一、vue:如何新建一个vue项目

    比较好用的一个脚手架:https://a1029563229.gitbooks.io/vue/content/cooking-cli.html 创建一个vue项目的流程: 1.安装node,版本号必须 ...

  7. 整理JavaScript循环数组和对象的方法

    众所周知,常用的循环语句有for.while.do-while.for-in,forEach以及jQuery中提供的循环的方法:以及ES6中提供的很多用来循环对象的方法如map, 在 Javascri ...

  8. 软工读书笔记 week 8 —— 《疯狂的程序员》

    这次接着上一次的进度继续阅读,并将其中感悟较深的几点记录如下.      程序员是一个幕后工作者 书中绝影给医院写软件,而医生(用户)只是评价这个软件好不好用,而不会去评价写这个软件的程序员优不优秀. ...

  9. leetCode题解 Reverse Words in a String III

    1.题目描述 Given a string, you need to reverse the order of characters in each word within a sentence wh ...

  10. 搜索关键字自动更正 - Oracle Endeca Server

    做了几个Oracle Endeca 电商项目.每个项目都会有搜过关键字拼写错误更正(Spelling Correction)的需求.淘宝也有类似功能. Oracle Endeca Sever提供了关键 ...