题目链接 965. 单值二叉树
bool isUnivalTree(struct TreeNode* root) {// if (root->left != NULL && root->right != NULL) {// return root->val == root->left->val// && root->val == root->right->val// && isUnivalTree(root->left)// && isUnivalTree(root->right);// }// else if (root->right != NULL && root->left == NULL) {// return root->val == root->right->val && isUnivalTree(root->right);// }// else if (root->left != NULL && root->right == NULL) {// return root->val == root->left->val && isUnivalTree(root->left);// }// else { // 没有孩子节点// return true;// }if (root == NULL || (root->left == NULL && root->right == NULL)) {return true;}else if ((root->left && root->left->val != root->val)|| (root->right && root->right->val != root->val)) {return false;}else {return isUnivalTree(root->left) && isUnivalTree(root->right);}
}