【leetcode】965. Univalued Binary Tree
题目如下:
A binary tree is univalued if every node in the tree has the same value.
Return
trueif and only if the given tree is univalued.Example 1:
Input: [1,1,1,1,1,null,1]
Output: trueExample 2:
Input: [2,2,2,5,2]
Output: falseNote:
- The number of nodes in the given tree will be in the range
[1, 100].- Each node's value will be an integer in the range
[0, 99].
解题思路:送分题。
代码如下:
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def recursive(self,node,v):
if node.val != v:
return False
r1 = r2 = True
if node.left != None:
r1 = self.recursive(node.left,v)
if node.right != None:
r2 = self.recursive(node.right,v)
return r1 and r2 def isUnivalTree(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if root == None:
return True
return self.recursive(root,root.val)
【leetcode】965. Univalued Binary Tree的更多相关文章
- 【LeetCode】965. Univalued Binary Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...
- 【Leetcode_easy】965. Univalued Binary Tree
problem 965. Univalued Binary Tree 参考 1. Leetcode_easy_965. Univalued Binary Tree; 完
- 【leetcode】998. Maximum Binary Tree II
题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
- 【LeetCode】226. Invert Binary Tree 翻转二叉树(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址: https://lee ...
- 【LeetCode】998. Maximum Binary Tree II 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 【LeetCode】971. Flip Binary Tree To Match Preorder Traversal 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前序遍历 日期 题目地址:https://leetc ...
- 【LeetCode】114. Flatten Binary Tree to Linked List 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先序遍历 递归 日期 题目地址:https://le ...
- 【LeetCode】919. Complete Binary Tree Inserter 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...
随机推荐
- Foobar 2000增加APE播放支持的方法
这里说明一下APE,它是一种常用的无损音乐的存储格式,通常会有将原始音乐光盘数字化后存储的APE文件搭配一个CUE文件使用.这个APE存储了音乐的原始数据,而CUE文件则是一个索引文件,用来标记音乐光 ...
- Electron-vue实战(一)—搭建项目与安装Element UI
Electron-vue实战—搭建项目与安装Element UI 作者:狐狸家的鱼 本文链接 GitHub:sueRimn 一.新建项目1.初始化项目打开cmd,新建一个项目,我使用的是electro ...
- PostgreSQL按年月日分组(关键词:extract time as Year/Month/Day)
Select EXTRACT(year from cast(joindate as timestamp)) as Year, EXTRACT(month from cast(joindate as t ...
- 【原理】scan
SCAN 命令的保证(guarantees) 同一个元素可能会被返回多次. 处理重复元素的工作交由应用程序负责, 比如说, 可以考虑将迭代返回的元素仅仅用于可以安全地重复执行多次的操作上. 如果一个元 ...
- (转)Uncaught TypeError: Cannot set property 'innerHTML' of null
(转)http://www.cnblogs.com/Ricky-Huang/p/5536253.html 在使用Ueditor的时候,会爆出这样的错误: 浏览器控制台就报错了 Cannot set p ...
- SPI驱动程序设计
一.SPI驱动子系统架构 m25p80.c: static int __devinit m25p_probe(struct spi_device *spi) { struct flash_platfo ...
- Spring 容器中bean的加载过程
bean 的加载过程大致可以分为以下几个步骤: 1.获取配置的资源文件 2.对获取到的xml资源文件进行解析 3.获取包装资源 4.解析处理包装之后的资源 5.加载 提取bean 并进行注册(添加到b ...
- [CSP-S模拟测试41]题解
中间咕的几次考试就先咕着吧…… A.夜莺与玫瑰 枚举斜率.只考虑斜率为正且不平行于坐标轴的直线,最后把$ans\times 2$再$+1$即可. 首先肯定需要用$gcd(i,j)==1$确保斜率的唯一 ...
- Linux桌面与命令行切换
1.首先在安装Linux的时候是选则Desktop桌面方式安装 2.切换命令 2.1快捷键:Ctrl+Alt+F1 切换到桌面模式 Ctrl+Alt+F3 切换到命令行模式
- Oracle10gr2 开机自启动脚本
目录 目录 软件环境 Oracle服务启动停止重启脚本 软件环境 操作系统 RHEL6.1 软件 Oracle10gr2 Oracle服务启动.停止.重启脚本 su - oracle cd /u01/ ...

