当前位置:网站首页>力扣解法汇总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;
}
}边栏推荐
- 竞价广告每次点击出价多少钱是固定的吗?
- Software engineering course: Chapter 2 software problem definition and feasibility analysis after class exercises
- Leetcode 55 jump game
- 决定广告质量的三个主要因素
- MySQL table common operation mind map
- Graphic data analysis | data cleaning and pretreatment
- RPA introduction
- The establishment and introduction of the announcement module of PHP development blog system
- PHP development 09 article module deletion and article classification writing
- How WPS inserts a directory and the operating steps for quickly inserting a directory
猜你喜欢

How to stop anti-virus software from blocking a web page? Take gdata as an example

Explore performance optimization! Performance improvement from 2 months to 4 hours!

kali安装empire过程中遇到的各种报错解决方案

Dataset how to use dataset gracefully. After reading this article, you can fully understand the dataset in c7n/choerodon/ toothfish UI

C language programming classic games - minesweeping

Various error reporting solutions encountered by Kali during Empire installation

Design practice of rongyun Im on electron platform

如何让杀毒软件停止屏蔽某个网页?以GDATA为例

Graphic data analysis | data cleaning and pretreatment

Don't miss it! Five large data visualization screens that HR must collect
随机推荐
[C language] C language file operation | C language file reading and writing | single character reading and writing | string reading and writing | format reading and writing | binary form reading and
Various error reporting solutions encountered by Kali during Empire installation
Manually tear the linked list (insert, delete, sort) and pointer operation
力扣编程题-解法汇总
Alicloud OSS file upload system
如何为Excel中的单元格自动填充颜色
MySQL表常用操作思维导图
消防栓监测系统毕业设计---论文(附加最全面的从硬件电路设计->驱动程序设计->阿里云物联网搭建->安卓APP设计)
Huawei, this is too strong
Tiobe - programming language ranking in June 2022
如何提高广告的广告评级,也就是质量得分?
Make ads more relevant by searching for additional information about ads
Fatal error in launcher: unable to create process using
微信公众号开发地理位置坐标的转换
ozzanimation-基于sse的动作系统
LeetCode Algorithm 1791. Find the central node of the star chart
入手Ticwatch2
Why do we use Google search ads?
小程序111111
Websocket is closed after 10 seconds of background switching