A binary tree is univalued if every node in the tree has the same value.
Return true if and only if the given tree is univalued.
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/univalued-binary-tree
/*取根结点作为参数,与其它所有结点比较,有不同值返回false */
bool dnf(struct TreeNode *root,int *x)
{
if(root==NULL)
return true;
if (root->val != *x)
return false;
return dnf(root->left, x) && dnf(root->right, x);
}
bool isUnivalTree(struct TreeNode* root){
int x = root->val;
return dnf(root->left, &x) && dnf(root->right, &x);
}