力扣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;
}
相关推荐
aqiu1111111 小时前
【C++算法打怪专栏】8. 字符串转换整数 (atoi)
开发语言·c++·算法
代码什么用10 小时前
Spring基础使用
java·后端·spring
信奥卷王10 小时前
[GESP202609 六级] 分树规划
算法
Su米苏10 小时前
Spring AI 中MCP 与普通 @Tool的区别
java·人工智能·spring
夏幻灵10 小时前
LeetCode 21:合并两个有序链表——双指针与哑节点详解
算法·leetcode·链表
码云数智-园园10 小时前
unique_ptr 还是 shared_ptr?C++ 智能指针选型与内存泄漏实战分析
java·开发语言
老板一杯拿铁10 小时前
Java 异常体系详解:从字节码原理到最佳实践
java
蜗牛互联网10 小时前
Java 17 HttpClient调用文件转写API的超时与失败回退
java·人工智能·后端
禾小西11 小时前
07丨Redis 哨兵机制:主库故障后,如何恢复服务?
java·开发语言·redis