把二叉树遍历,然后把每一个数都存入数组,然后再遍历数组即可:
c
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
void Preorder_trave(struct TreeNode* root,int* number,int* length)
{
if(root == NULL)
{
return;
}
number[(*length)++] = root->val;
Preorder_trave(root->left,number,length);
Preorder_trave(root->right,number,length);
}
bool isUnivalTree(struct TreeNode* root) {
int* number = (int*)malloc(sizeof(int) * 100);
int* length = (int*)malloc(sizeof(int));
*length = 0;
Preorder_trave(root,number,length);
int i = 0;
int k = *length;
for(i = 0;i < k - 1;i++)
{
if(number[i] != number[i + 1])
{
return false;
}
}
return true;
}