力扣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;
}
相关推荐
WBluuue10 分钟前
Codeforces 1087 Div2(ABCDEF)
c++·算法
Kiling_070411 分钟前
Java多态、final与抽象类:面向对象编程进阶指南
java·开发语言
better_liang31 分钟前
每日Java面试场景题知识点之-MySQL索引
java·数据库·mysql·性能优化·索引
Yzzz-F35 分钟前
2025 ICPC武汉邀请赛 G [根号分治 容斥原理+DP]
算法
abant240 分钟前
leetcode 114 二叉树变链表
算法·leetcode·链表
tankeven42 分钟前
HJ165 小红的优惠券
c++·算法
Aktx20FNz44 分钟前
一文学习 Spring AOP 源码全过程
java·学习·spring
Nyarlathotep01131 小时前
ThreadLocal
java·后端
先积累问题,再逐次解决1 小时前
快速幂优美算法
算法