【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 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. ...
随机推荐
- 浅谈CICD持续集成、持续部署的流程(转)
Jenkins是一个比较流行的持续集成工具GitLab是存储镜像的镜像仓库由客户端将代码push推送到git仓库,gitlab上配置了一个webHook的东西可以触发Jenkins的构建.进入到Jen ...
- 数据结构---Java---Hastable
1.概述 1.1 Hashtable是线程安全的: 1.2 源码 public class Hashtable<K,V> extends Dictionary<K,V> imp ...
- Hashtable、HashMap、TreeMap、ConcurrentHashMap、ConcurrentSkipListMap区别
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11444013.html 并发场景下的Map容器使用场景 如果对数据有强一致要求,则需使用Hashtab ...
- Delphi 字符串函数SysUtils单元 AnsiSameStr、AnsiSameText、AnsiCompareStr、AnsiCompareText、AnsiCompareFileName、AnsiUpperCase、AnsiLowerCase、AnsiUpperCaseFileName、AnsiLowerCaseFileName、AnsiPos、AnsiQuotedStr
USES 单元 SysUtils 非 StrUtils AnsiSameStr.AnsiSameText.AnsiCompareStr.AnsiCompareText.AnsiCompareFileN ...
- SQL语句之-函数
六.函数 1.文本处理函数 2.日期和时间处理函数 MySQL数据库:SELECT * FROM orders WHERE YEAR(order_date)=2012 七.汇总数据 1.AVG()函 ...
- gulp压缩css
gulp压缩css,选用的依赖是gulp-clean-css,在压缩大型项目时还对用到一个dom流压缩文件选取的依赖gulp-dom-src 依赖安装:npm i gulp-clean-css 依赖安 ...
- 2的N次方求解-----C++
2的N次方求解,一般情况如果不超出C/C++基本数据类型的表达范围,这个问题及其容易,但是如果N的值十分的大,以致于超出基本数据类型表达范围 下面的程序正是解决2的N次方这个大数精确求解的源码 #in ...
- java.lang.String中的replace方法到底替换了一个还是全部替换了。
你没有看错我说的就是那个最常用的java.lang.String,String可以说在Java中使用量最广泛的类了. 但是我却发现我弄错了他的一个API(也可以说是两个API),这个API是关于字符串 ...
- 58、salesforce学习笔记(五)
Set集合 Set<String> set1 = new Set<String>(); set1.add('1'); set1.add('2'); Set<String& ...
- layui弹出层回调的使用
<%@page language="java" contentType="text/html; charset=UTF-8"%> <%@ in ...