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;
}
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » 力扣965 单值二叉树
发表评论 取消回复