力扣965 单值二叉树

Here is the code for you to see directly, however, there are a few points.

1. in the second 'if' clause, note the existence of the root node

2. Once there is at least one false value among the serval judgment values, the result after the && operation is false.

复制代码
bool isUnivalTree(struct TreeNode* root) {
	if (root == NULL)
		return true;
	if (root->left && root->left->val!= root->val)
		return false;
	if (root->right&&root->right->val!=root->val)
		return false;
	bool a = isUnivalTree(root->left) && isUnivalTree(root->right);
	return a;
}
相关推荐
资深web全栈开发4 分钟前
贪心算法套路解析
算法·贪心算法·golang
武子康5 分钟前
Java-170 Neo4j 事务、索引与约束实战:语法、并发陷阱与速修清单
java·开发语言·数据库·sql·nosql·neo4j·索引
~~李木子~~6 分钟前
贪心算法实验2
算法·贪心算法
q***23578 分钟前
在2023idea中如何创建SpringBoot
java·spring boot·后端
FanXing_zl13 分钟前
快速掌握线性代数:核心概念与深度解析
线性代数·算法·机器学习
7澄126 分钟前
Java 实战:投票统计系统(HashMap 应用)
java·开发语言·intellij-idea·交互·控制台·hashmap
zzzsde27 分钟前
【C++】红黑树:使用及实现
开发语言·c++·算法
q***787827 分钟前
SpringBoot后端服务重定向
java·spring boot·后端
Fantasydg41 分钟前
外卖项目day02
java