Univalued Binary Tree
Problem
A binary tree is uni-valued if every node in the tree has the same value.
Given the
root of a binary tree, return true
if the given tree is uni-valued, or false
otherwise.
Constraints
- The number of nodes in the tree is in the range
[1, 100]
. 0 <= Node.val < 100
Solution
The problem Univalued Binary Tree
can be solved using a depth-first search to compare all node values.
Implementation
static const int fast_io = []()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
return 0;
}();
/**
* 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 helper(TreeNode *root, int val)
{
if (root == NULL)
return true;
return root->val == val && helper(root->left, val) && helper(root->right, val);
}
bool isUnivalTree(TreeNode *root) { return helper(root, root->val); }
};