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) { ...
随机推荐
- POJ3278(KB1-C 简单搜索)
Catch That Cow Description Farmer John has been informed of the location of a fugitive cow and wants ...
- 用数组指针遍历数组,FOR/FOREACH遍历数组
1. 用数组指针遍历一维数组 <?php header("Content-type:text/html;charset=utf-8"); /*用数组指针遍历一位数组的值*/ ...
- 【PyQt5 学习记录】005:QMainWindow 及状态栏、菜单栏和工具栏
#!/usr/bin/env python import sys from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QA ...
- 前台提交数据(表单数据、Json数据及上传文件)的类型
MIME (Multipurpose Internet Mail Extensions) 是描述内容类型的互联网标准.Clients use this content type or media ty ...
- PS改变图像颜色
由于写的一个页面主色调变了,里面的一些图标颜色也要相应改变,自己难得重新去psd里面截图,就想着用ps,看能否直接能变换一下图标颜色.其实方法也很简单的. 1:用ps打开需要改变图标颜色的文件,然后选 ...
- eventbus3-intellij-plugin插件搜不到
一.eventbus3-intellij-plugin插件搜不到
- 2Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
public class TestException { public static void main(String[] args) { String str = "1"; fo ...
- Linux服务器安装redis数据库教程
前面小Alan给大家说了jdk的安装,这篇跟大家聊聊redis非关系型数据库在Linux服务器的安装. redis简单介绍 REmote DIctionary Server(Redis) 是一个由Sa ...
- 解决 There are no resources that can be added or removed from the server
网上下载了一个项目,在eclipse中部署时,加载项目到tomcat中项目名称无法显示,报出There are no resources that can be added or removed fr ...
- JavaScript DOM 編程藝術(2版) 綜合實例Band js代碼
function addLoadEvent(func){ var oldonload=window.onload; if(typeof window.onload!='function') { win ...