1005. Univalued Binary Tree
My accepted C++ solution to LeetCode problem 1005, Univalued Binary Tree, running in 0ms.
- Difficulty: Easy
- C++
- Runtime 0ms
- Memory 12.5MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
C++
Accepted on LeetCode — runtime 0ms, memory 12.5MB, accepted 2025-12-27.
class Solution {
public:
bool isUnivalTree(TreeNode* root) {
if (!root) return true;
if (root->left && root->left->val != root->val) return false;
if (root->right && root->right->val != root->val) return false;
return isUnivalTree(root->left) && isUnivalTree(root->right);
}
};