【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. ...
随机推荐
- 理解Java主函数中的"String[] args"
public class Understand_String_args { public static void main(String[] args) { System.out.printf(&qu ...
- Quartz.Net 任务调度之特性(3)
再实现类中使用,就是继承IJob的类 [PersistJobDataAfterExecution] //执行后的保留作业数据,链式传参(上一次的任务数据) [DisallowConcurrentExe ...
- 【leetcode】701. Insert into a Binary Search Tree
题目如下: Given the root node of a binary search tree (BST) and a value to be inserted into the tree, in ...
- c++11 继承构造函数
若基类拥有数量众多的不同版本的构造函数,而派生类中只有一些成员函数,则对于派生类而言,其构造函数就等同于构造基类. struct A { A(int i) {} A(double d, int i) ...
- implements Serializable有什么作用
转自 http://blog.csdn.net/dinghqalex/article/details/46009911
- HTML CSS + DIV实现排版布局
HTML CSS + DIV实现排版布局 1.网页可以看成是由一个一个"盒子"组成,如图: 由上图可以看出,页面分为上(网站导航).中.下(版权声明)三个部分,中间部分又分为左(商 ...
- javaweb登录验证码的实现
第一种 第一步: JSP <li><input name="validCode" id="validCode" type="te ...
- CDQ分治&整体二分学习个人小结
目录 小结 CDQ分治 二维LIS 第一道裸题 bzoj1176 Mokia bzoj3262 陌上花开 bzoj 1790 矩形藏宝地 hdu5126四维偏序 P3157 [CQOI2011]动态逆 ...
- table td 溢出隐藏
需要给table加一个属性:table-layout:fixed;
- UVA1152_4 Values whose Sum is 0
中途相遇法,这题目总结后我感觉和第一篇博客很像,他们都取了中间,也许这就是二分的魅力吧 这题题意就是从ABCD四个集合中选四个元素使他们的和为0 题意很简单,但是实现起来很容易超时,不能一个一个枚举 ...

