解题思路:
从根出发,判断左右两节点值是否相等,然后递归左右子节点执行相同操作,出现不同返回false,代码如下:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool isUnivalTree(TreeNode* root) {
if(root == nullptr) return true;
if(root->left != nullptr && root->val != root->left->val) {
return false;
}
if(root->right != nullptr && root->val != root->left->val)
}
};