当前位置:网站首页>力扣解法汇总965-单值二叉树
力扣解法汇总965-单值二叉树
2022-06-12 02:03:00 【失落夏天】
目录链接:
力扣编程题-解法汇总_分享+记录-CSDN博客
GitHub同步刷题项目:
原题链接:力扣
描述:
如果二叉树每个节点都具有相同的值,那么该二叉树就是单值二叉树。
只有给定的树是单值二叉树时,才返回 true;否则返回 false。
示例 1:
输入:[1,1,1,1,1,null,1]
输出:true
示例 2:
输入:[2,2,2,5,2]
输出:false
提示:
给定树的节点数范围是 [1, 100]。
每个节点的值都是整数,范围为 [0, 99] 。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/univalued-binary-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
* 解题思路: * 递归遍历,如果不等于root的值,则返回false。
代码:
public class Solution965 {
public boolean isUnivalTree(TreeNode root) {
return isUnivalTree(root, root.val);
}
public boolean isUnivalTree(TreeNode root, Integer value) {
if (root.val != value) {
return false;
}
if (root.left != null && !isUnivalTree(root.left, value)) {
return false;
}
if (root.right != null && !isUnivalTree(root.right, value)) {
return false;
}
return true;
}
}边栏推荐
猜你喜欢

php开发 博客系统的公告模块的建立和引入

On the night of the joint commissioning, I beat up my colleagues

Operating mechanism of Google ads bidding

Summary of concrete (ground + wall) + Mountain crack data set (classification and target detection)

Glfwpollevents() program crash

Alicloud OSS file upload system

Installing MySQL version 5.5 database for Linux (centos6)

Smartbi helps you solve the problem of losing high-value customers

Software engineering - system flow chart

Quatre schémas de mise en file d'attente des messages pour redis
随机推荐
Basedexclassloader
JSON conversion: entity classes and jsonobject are converted to each other, and list and jsonarray are converted to each other (fastjson version)
Linux(CentOS7)安装MySQL-5.7版本
Comprehensive quality of teaching resources in the second half of 2019 - subjective questions
android html5页面加载缓存优化
MySQL表常用操作思维导图
Basic use of MATLAB
Invert words in a string (split, double ended queue)
MySQL table common operation mind map
力扣解法汇总497-非重叠矩形中的随机点
Knowledge points of mall development
Is there a female Bluetooth headset suitable for girls? 38 Bluetooth headsets worth getting started
How to automatically color cells in Excel
自适应搜索广告有哪些优势?
Fatal error in launcher: unable to create process using
Wide match modifier symbol has been deprecated, do not use
Pyinstaller packaging Exe (detailed tutorial)
How to maximize the use of various matching methods—— Google SEM
PHP security development 13 column module of blog system
Metaverse × How will smart cities develop?